Categories
JavaScript Answers

How to use regex to get all the characters after a specific character with JavaScript?

Spread the love

Sometimes, we want to use regex to get all the characters after a specific character with JavaScript.

In this article, we’ll look at how to use regex to get all the characters after a specific character with JavaScript.

How to use regex to get all the characters after a specific character with JavaScript?

To use regex to get all the characters after a specific character with JavaScript, we use the string substr and indexOf methods.

For instance, we write

const str = "'SELECT___100E___7',24";
const afterComma = str.substr(str.indexOf(",") + 1);

to call indexOf to get the index of the first comma.

Then we call substr with str.indexOf(",") + 1 to return the substring in str after the first comma.

Conclusion

To use regex to get all the characters after a specific character with JavaScript, we use the string substr and indexOf methods.

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 *