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
It’s right to use the capture group in the regular expression. In addition, expand your ideas:
replace
The method can be used in combination with the capture group, which is very powerful.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.
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