Ok, i have a regex pattern like this /^([SW])w+([0-9]{4})$/
This pattern should match a string like SW0001
with SW
-Prefix and 4 digits.
I thougth [0-9]{4}
would do the job, but it also matches strings with 5 digits and so on.
Any suggestions on how to get this to work to only match strings with SW
and 4 digits?
Advertisement
Answer
Let’s see what the regex /^([SW])w+([0-9]{4})$/
match
- Start with S or W since character class is used
- One or more alphanumeric character or underscore(
w
=[a-zA-Z0-9_]
) - Four digits
This match more than just SW0001
.
Use the below regex.
/^SWd{4}$/
This regex will match string that starts with SW
followed by exactly four digits.