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?
Look at it like this
/\(([^()]+)\)/g
/[^()]+(?=))/g
, after personal testing, it can meet the needs of the question ownerThe return value of the
So, after using - g
match
function is related to whether the regular expression used contains theg
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 theg
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 byComplete substring
,matches the third complete substring
...so the length of the array isthe 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.