Categories
JavaScript Answers

How to extract “456” from “abc_456” where abc is of indefinite length with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *