[Javascript] What is the regular expression to extract string between two parenthesis?


I want to extract string present between two parenthesis, How can I do that in Javascript?

1 Answer

3 years ago by

You can use either of the below regular expressions to extract the substring present between two paranthesis

let str = "cost of two mangoes is ($10)";

let regExp1 = /\(([^)]+)\)/;
console.log(regExp1.exec(str)[1]); // [1] contains the value between the parenthesis.

let regExp2 = /\((.*)\)/;
console.log(str.match(regExp2).pop());

Run here https://onecompiler.com/javascript/3xnrmphrj

3 years ago by Meera