Javascript regex: is there anyway to write a regex which gives true if backreference is NOT matched -
so here problem: i'm checking input of 2 years hyphen. like:
2001-2015
to test this, use simple regex
/^([0-9]{4})-([0-9]{4})$/
i know groups aren't needed, , (19|20)[0-9]{2}
, closer match basic year exp, bear me.
now, if requirement match 2 years if same, have used backreference like:
/^([0-9]{4})-\1$/
which matches 2000-2000
not 2000-2014
my actual requirement opposite. want match if years different not if they're same. is, 2000-2014
should match. 2000-2000
should not.
and using negative of boolean find not option. need huuuge regex supposed match whole lot of different date formats. part of it.
is there way achieve this?
you can use negative lookahead achieve this:
^([0-9]{4})-(?!\1)[0-9]{4}$
this same pattern, except inserts condition check using backreference.
(?!\1)
fail if \1
matches @ position.
Comments
Post a Comment