javascript - Asking for help regarding js regular issues
某草草
某草草 2017-05-16 13:39:37
0
4
432

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

某草草
某草草

reply all(4)
世界只因有你

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.

習慣沉默

You use the capture group to get it

为情所困

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);
}
给我你的怀抱

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!