javascript - Regex to match begining of string with set endings or not -
looking regex match whatever @ beginning of string minus 1 of following set endings:
_a _b _c _zz
so example:
1. blah needs match blah 2. blah_a needs match blah 3. blahblah_a needs match blahblah 4. blah_d needs match blah_d 5. blah_z needs match blah_z 6. blah_zz needs match blah 7. blah_ablah needs match blah_ablah
unsuccessful attemps have tried are:
.*(?=(_(a|b|c|zz))) did not match 1,4,5, , 7 correctly .*([^(_a$)]|[^(_b$)]|[^(_c$)]|[^(_zz$)]) did not match correctly
regex vesrion used javascript
anyone got ideas?
the problem .*
can consume entire string (and will). simplest fix make repetition ungreedy , require end of string after optional _
endings:
.*?(?=(_(a|b|c|zz))?$)
this way .*
won't consume ending characters if should excluded.
as minor optimisation, avoid capturing group (if don't need it) , use character class, single-character endings:
.*?(?=(?:_(?:[abc]|zz))?$)
Comments
Post a Comment