user726049
user726049

Reputation:

How to require the whole input string to match the regex pattern

I have the following RegEx:

/[a-zA-Z\d ']{1,30}/

and the following string:

some text'&&

Now, that RegEx returns true on the string. I suppose that it matches the part without "&&". I'd like to ask how can I limit characters to alphanumeric characters, including space and apostrophes as the RegEx from above writes.

Upvotes: -1

Views: 40

Answers (1)

Amber
Amber

Reputation: 526613

Anchor your regex:

/^[a-zA-Z\d ']{1,30}$/

^ means "start of string" and $ means "end of string" so adding those markers forces the regex to either match the entire string or not at all.

Upvotes: 5

Related Questions