Finding parenthesis via regular expression

后端 未结 3 1311
情话喂你
情话喂你 2021-01-26 04:51

I am not great with regular expressions. I am looking to find if a string contains \"( ),[ ], { }\". Note, I am not looking for the contents in the actual ( ), just to see if

3条回答
  •  故里飘歌
    2021-01-26 05:24

    Use alternatives with a non-greedy match:

    /\(.*?\)|\{.*?\}|\[.*?\]/
    

    Without those question marks, the patterns .* would be "greedy", eg scanning "abc(def)ehi(jkl)mno" would find only one match of "(def)ehi(jkl)" (the .* would gobble up everything to the last close bracket), but using non-greedy .*? you would get two matches "(def)" and "(jkl)" as you would want.

提交回复
热议问题