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

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. matchfunction is related to whether the regular expression used contains thegflag;
          If there is nogflag, 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 isThe number of capturing brackets + 1;If there is the
          gflag and if the string matches, the return result is an array. The elements of the array are the first complete substring matched byand 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;

        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
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!