Remove the contents of all brackets in the string. There must be no brackets inside the brackets
var str = "dskf(AAA)_8hjk(CCC)dsk(BBB)";
var reg = /(?:\()\w+(?:\))/g;
var res = str.match(reg);
//["(AAA)", "(CCC)", "(BBB)"]
The result you get has parentheses on both sides. Don’t you want the parentheses?
(?:exper) Isn’t this a non-obtaining match?
世界只因有你2017-06-26 10:58:48
/\(([^()]+)\)/g
> s='dskf(AAA)_8hjk(CCC)dsk(BBB)'
> p=/\(([^()]+)\)/g
> while (r=p.exec(s)){console.log(r[1])}
AAA
CCC
BBB
学习ing2017-06-26 10:58:48
/[^()]+(?=))/g
, after personal testing, it can meet the needs of the question owner
阿神2017-06-26 10:58:48
match
function is related to whether the regular expression used contains the g
flag;
If there is no g
flag, if the string matches, the returned result is an array, and the elements of the array are respectively It is the complete substring matched by ,
the content of the first capturing bracket ,
the content of the second capturing bracket ,
the content of the third capturing bracket ... so the length of the array is
The number of capturing brackets + 1;
If there is the
g flag and if the string matches, the return result is an array. The elements of the array are the first complete substring matched by
and the second matched by Complete substring
, matches the third complete substring
...so the length of the array is the number of matches
; If there is no match, return null;
, the result will only return the complete substring matched by , and will not include the content of the capturing brackets. For your needs, the match function should not be able to do it.