Regex to match at least n times but not more than m times -


i want regular expression match string %*--- @ least 1 hyphen expression should not matched if there more 3 hyphens.![enter image description here][1] far have come /^%?\*{1}\s*(\- *){1,3}/ still matching when hyphens exceed 3.

i have tried ? after range {1,3} not meeting requirement.

although it's written {min,max} in tutorials , references, enumerated quantifier not mean not more max. if sees 3 hyphens, -{1,3} consume three, doesn't care next character (if there one). it's other quantifiers in regard: consumes as can, hands control next part of regex.

that's why other responders suggested using end anchor ($). if can't use anchor, or don't want to, can use negative lookahead instead:

/^%\*-{1,3}(?!-)/ 

Comments