javascript - js regular expression to get the content inside the brackets
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-06-26 10:56:48
0
4
1084

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?

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(4)
習慣沉默
/[^(][a-zA-Z0-9]+(?=\))/g

Look at it like this

世界只因有你

/\(([^()]+)\)/g

> s='dskf(AAA)_8hjk(CCC)dsk(BBB)'
> p=/\(([^()]+)\)/g
> while (r=p.exec(s)){console.log(r[1])}
AAA
CCC
BBB
学习ing

/[^()]+(?=))/g, after personal testing, it can meet the needs of the question owner

阿神
    The return value of the
  1. 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;

  2. So, after using
  3. g

    , 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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template