Home  >  Q&A  >  body text

javascript - js regular expression to get the content inside the brackets

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?

曾经蜡笔没有小新曾经蜡笔没有小新2672 days ago1027

reply all(4)I'll reply

  • 習慣沉默

    習慣沉默2017-06-26 10:58:48

    /[^(][a-zA-Z0-9]+(?=\))/g

    Look at it like this

    reply
    0
  • 世界只因有你

    世界只因有你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

    reply
    0
  • 学习ing

    学习ing2017-06-26 10:58:48

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

    reply
    0
  • 阿神

    阿神2017-06-26 10:58:48

      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.

      reply
      0
  • Cancelreply