I need to check whether information entered are 3 character long, first one should be 0-9 second A-Z and third 0-9 again.
I have written pattern as below:
var pattern = `'^[A-Z]+[0-9]+[A-Z]$'`;
var valid = str.match(pattern);
I got confused with usage of regex for selecting, matching and replacing.
- In this case, does
[A-Z]check only one character or whole string ? - Does
+separate(split?) out characters?
1)
+matches one or more. You want exactly one2) declare your pattern as a REGEX literal, inside forward slashes
With these two points in mind, your pattern should be
Note also you can make the pattern slightly shorter by replacing
[0-9]with the\dshortcut (matches any numerical character).3) Optionally, add the case-insensitive
iflag after the final trailing slash if you want to allow either case.4) If you want to merely test a string matches a pattern, rather than retrieve a match from it, use
test(), notmatch()– it’s more efficient.