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!
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .
To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.
The RegExp \D Metacharacter in JavaScript is used to search non digit characters i.e all the characters except digits. It is same as [^0-9].
An empty regular expression matches everything.
var str = ('nisi non text600 elit').match(/text(\d+)/);
alert(str[1]);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With