Home > Blockchain >  Regex to test if string contains two forward slash or 2 hyphens
Regex to test if string contains two forward slash or 2 hyphens

Time:07-02

I am trying to create some regex that can test whether or not a string contains 2 forward slashes or two hypens for dates.

These would pass: 10/25/1984 2005/06/24 1882-03-14 05-29-1932

Here is the regex that is failing:

/[\-\/{2,}/g

CodePudding user response:

Either capture and check for backreference... (demo)

([-\/]).*?\1

Or simply use an alternation (demo)

-[^-]*-|\/[^\/]*\/

[^...] represents a negated character class.

  • Related