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.