Sometimes, we want to extract "456" from "abc_456" where abc is of indefinite length with JavaScript.
In this article, we’ll look at how to extract "456" from "abc_456" where abc is of indefinite length with JavaScript.
How to extract "456" from "abc_456" where abc is of indefinite length with JavaScript?
To extract "456" from "abc_456" where abc is of indefinite length with JavaScript, we can use the string match
method.
For instance, we write
const pat = /([0-9]{1,})$/;
const m = str.match(pat);
to call str.match
with the pat
regex to return an array of number matches in the str
string.
We use [0-9]{1,}
to match numbers.
And we put it before $
to get numbers at the end of the string.
Conclusion
To extract "456" from "abc_456" where abc is of indefinite length with JavaScript, we can use the string match
method.