Reputation: 8659
Good Day,
I'm creating a javascript function to exclude certain codes from the database. For example, the following are valid codes:
In other words, I want the codes to consist of only alphanumerics. I opened up the console in Chrome and tried:
> re = new RegExp('\w+')
> re.test('whatever')
true
> re.test('what???')
true
> var test = new RegExp('^\w+')
> test.test('what')
true
> test.test('what999')
true
> test.test('what???')
true
So I know that \w
can either be a 0-9, a-z, A-Z. I don't know why the regex is passing if I enter '?' when they shouldn't.
Am I missing something?
Upvotes: 1
Views: 1245
Reputation:
You're misinterpreting your results. The regexp \w+
means "one or more word characters". It doesn't specify where in the string these characters can be found. In all of your tests, the provided string contains at least one word character, so they all pass.
What you're meaning to do is ensure the string contains only alphanumerics. Try the following regex:
^\w+$
Broken down, this means:
^ = match start of string
\w = letters or digits
+ = one or more of the previous element (in this case, the set) (this is greedy)
$ = match the end of the string
In English, this means, "between the start and end of the string, only alphanumeric characters will match. Match as many as possible, or none at all"
Note: if you write your regex as a string, you need to escape the \
like so:
new RegExp("^\\w+$")
Otherwise, JavaScript will interpret \w
as an escape sequence, which it is not. You can also use the syntax
new RegExp(/^\w+$/)
in which case you don't need to escape the \
, since it isn't a string.
Upvotes: 2
Reputation: 1039
Test is returning true because the pattern \w+
is matching the alphanumeric part in your test strings.
re.test('what???')
for example will return true because it matches what
.
If you only want to match strings consisting of only alphanumeric characters, you should use something like ^\w+$
.
Upvotes: 0