Javascript: Using Regex To Search For Any Four Digits In A Row
I am trying to create a function that confirms if a string contains any four digits in a row (for the purposes of finding a date). The code I have is: string.search(^\d{4}$) Any a
Solution 1:
What you want is
/\d{4}/.test(yourString)
which returns a boolean.
Note that
- the ^ and $ you were using match the start and end of the string. They would have been useful to test if a string was only made of 4 digits.
search
is useful when you want the position of the match. In your casetest
is simpler.
To go further, I recommend this good and concise documentation on regexes in JavaScript
Post a Comment for "Javascript: Using Regex To Search For Any Four Digits In A Row"