Reputation: 1215
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 alerts text600, how can I alert only 600 without an additional replace of the word text(if that is possible)?
Any help is appreciated, thank you!
Upvotes: 5
Views: 338
Reputation: 919
Please see this working jsFiddle. You need to capture something in the regular expression, which is done using the (
and )
.
The result will be an array there index 0
is the whole match applied on the string and the following indicies are your captures.
Upvotes: 0
Reputation: 700562
Use parentheses to catch a group in the regular expression:
var str = ('nisi non text600 elit').match(/text(\d+)/)[1];
Note: A regular expression literal is not a string, so you shouldn't have apostrophes around it.
Upvotes: 5
Reputation: 27765
var str = ('nisi non text600 elit').match(/text(\d+)/);
alert(str[1]);
Upvotes: 6