Javascript regex

string = 'Hello 1234_ world 4567_ trap 456'; 

I need to write down all the numbers followed by the underscore. The following code will do.

 string.match(/(\d+?)(_)/gi); 

However, I tested the following code and it worked, except that the underscore was also fixed.

 (\d+)_ 

So I decided to emphasize my own capture group, like this

(\ d +) (_)

But that did not work. I get numbers with a final underscore. I do not want to stress.

+4
source share
2 answers

The matching method returns a string containing characters that have been matched regardless of whether they are part of a (capturing or not capturing) group.

A group (?=_) Is a view. Appearance is a match with zero width, and therefore it does not match any characters. It matches an empty string, but only if the character immediately after that is an underscore.

Groups are not really important. When you use a zero width match, the result will not contain any additional characters.

+4
source

Try

 var regex = /(\d+?)(_)/ 
0
source

Source: https://habr.com/ru/post/1303037/


All Articles