JavaScript (regex): excluding results matching

I have this text: nisi non text600 elit , where 600 is added dynamically. How can i get it?

var str = ('nisi non text600 elit').match(/text\d+/);
alert(str);

This warns text600 , how can I warn only 600 without further replacing the word text (if possible)?

Any help is appreciated, thanks!

+5
source share
4 answers
var str = ('nisi non text600 elit').match(/text(\d+)/);
alert(str[1]);
+6
source

Use parentheses to search for a group in a regular expression:

var str = ('nisi non text600 elit').match(/text(\d+)/)[1];

Note. A regular expression literal is not a string, so you should not have apostrophes.

+5
source

, jsFiddle. - , ( ).

The result will be an array, there the index 0is all the match applied to the string, and the following signs are your captures.

0
source
alert(str.substr(14, 16) )

try it

-1
source

All Articles