How To Search Regex Only Outside Curly Brackets
I have this regex variable: var regexp = new RegExp(RegExp.quote(myExpression) + '\\b', 'g'); which searches for expression that has a space after it. (RegExp.quate() I got from t
Solution 1:
Something like this problem of matching nested brackets is not possible to solve using a single regex. Here is my take to resolve your problem:
var myExpression = "cat";
var s = 'the cat {is cat { and} cat {and { another cat}}and cat } and another cat';
arr = s.split(/(?=(?:\b|\W))\s*/g);
document.writeln("<pre>split: " + arr + "</pre>");
//prints: the,cat,{,is,cat,{,and,},cat,{,and,{,another,cat,},},and,cat,},and,another,cat
var level=0;
for (i=0; i<arr.length; i++) {
if (level == 0 && arr[i] == myExpression)
document.writeln("<pre>Matched: " + arr[i] + "</pre>");
if (arr[i] == "{")
level++;
elseif (arr[i] == "}")
level--;
}
Solution 2:
One strategy would be to find/replace all {.*} with empty string... then find all the cats?
Post a Comment for "How To Search Regex Only Outside Curly Brackets"