Jquery get substring between parenthesis

2019-09-04 12:31发布

问题:

I have string like

"Shabalu Sheedaa - Ali Gul Panara - Octa School - 10 (7)"

and I want to extract the value between parenthesis using jquery. In this case it will be 7.

回答1:

Try:

var regExp = /\(([^)]+)\)/;
var matches = regExp.exec("Shabalu Sheedaa - Ali Gul Panara - Octa School - 10 (7)");

//matches[1] contains the value between the parentheses
console.log(matches[1]);

Explation of Reg Exp:

Breakdown:

\( : match an opening parentheses
( : begin capturing group
[^)]+: match one or more non ) characters
) : end capturing group
\) : match closing parentheses