Categories
JavaScript Answers

How to get text between two rounded brackets with JavaScript?

Spread the love

Sometimes, we want to get text between two rounded brackets with JavaScript.

In this article, we’ll look at how to get text between two rounded brackets with JavaScript.

How to get text between two rounded brackets with JavaScript?

To get text between two rounded brackets with JavaScript, we use the match method.

For instance, we write

const txt = "This is (my) simple text";
const re = /\((.*)\)/;
const [, match] = txt.match(re);

to use the /\((.*)\)/ regex to match the text between the parentheses.

Then we call txt.match with re to return the text between the parentheses by getting the 2nd entry from the array.

Conclusion

To get text between two rounded brackets with JavaScript, we use the 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 *