user557108
user557108

Reputation: 1215

JavaScript(regex): excluding matches from output

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

Answers (4)

GodLesZ
GodLesZ

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

Guffa
Guffa

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

mohana rao
mohana rao

Reputation: 429

alert(str.substr(14, 16) )

Try this

Upvotes: -1

antyrat
antyrat

Reputation: 27765

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

Upvotes: 6

Related Questions