Reputation: 19
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 advice? I'm really inexperienced with Regex. I'd also appreciate any input on where to learn to use Regex more effectively.
Upvotes: 1
Views: 895
Reputation: 382112
What you want is
/\d{4}/.test(yourString)
which returns a boolean.
Note that
search
is useful when you want the position of the match. In your case test
is simpler.To go further, I recommend this good and concise documentation on regexes in JavaScript
Upvotes: 5