user12345 user12345 - 1 month ago 14
Javascript Question

Regex to scan for a specific number of a repeating character

First time using regex I am having a little bit of trouble formatting to select a specific count of

}


I want to be able to eliminate for example
}}}
but should still pick up
}}
.

I have this so far

/^(?!\||}}})(([A-Za-z0-9\s:,']|{{{|{{|}}|{:|:}|)+)/


however this way in the event of
x}}}
it will match
x}}
when I need to match
x
only. while if I submit
x}}
the return should be
x}}
.

Sample input:

example 1:
x}}}
output:
x
.

example 2:
x}}
output:
x}}

Answer

You might want to try this:

/((?!\})[^\}\{]*(?=\}{3}[^\}]?))|((?!\})[^\}\{]*\}{2}(?=[^\}]?))/g

var str = "{{{x}}{{x1}}{{x2}}}";
var regxPat = /((?!\})[^\}\{]*(?=\}{3}[^\}]?))|((?!\})[^\}\{]*\}{2}(?=[^\}]?))/g;
var matchResult = str.match(regxPat);

console.log(matchResult);

Comments