Home  >  Q&A  >  body text

javascript - Asking for help regarding js regular issues

In many cases, we only need to match the content in the middle of the expression, and the content at the matching position is not needed, such as the left and right brackets in the figure below. I know that zero-width assertions can be done, but js does not support reverse assertions. , is there any other regular method to handle it in one step? It is best not to post the plan for subsequent string interception and processing

某草草某草草2688 days ago437

reply all(4)I'll reply

  • 世界只因有你

    世界只因有你2017-05-16 13:41:37

    It’s right to use the capture group in the regular expression. In addition, expand your ideas:
    replaceThe method can be used in combination with the capture group, which is very powerful.

    "transform: scale(-12312.212) rotate(-0.23deg) translateX(0.31px);"
    .replace(/[^(]*\((-?\d+\.?\d+\w+?)\)[^(]*/g, function(m, p) {
        return p+' ';
    });
    // "-12312.212 -0.23deg 0.31px "

    For more knowledge about js regular system, it is recommended to read in depth the regular expression front-end manual. I have been writing this article intermittently for two months. If you hit me if it is not useful, I promise not to fight back.

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 13:41:37

    You use the capture group to get it

    reply
    0
  • 为情所困

    为情所困2017-05-16 13:41:37

    Group in regular expressions can achieve your needs.
    On the basis of the regular expression you wrote, add a pair of brackets (indicating a Group and expanding the part you really want). ((-?d+.?d+([a-z]+)?))

    Test code, where the myRegexp.exec method returns an array, and the first element is the string matched by the regular expression, including the brackets you "don't want to see". The second element (that is, the subscript is 1) corresponds to the content captured by the first bracket in your regular expression, which is the number and unit part you want.

    var myString = 'scale(-12312.212)skdk, rotate(-0.23deg)..+-(800)-555-2468';
    var myRegexp = /\((-?\d+\.?\d+([a-z]+)?)\)/g;
    match = myRegexp.exec(myString);
    while (match != null) {
      console.log(match[1])
      match = myRegexp.exec(myString);
    }

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:41:37

    Use regular grouping to get it done
    First use /g to match all the target strings
    Then use regular expression without /g for each one to get one group of the results, which is the value you need, go directly Code
    Surrounded by parentheses it is a capture group, please google for detailed usage

    reply
    0
  • Cancelreply