Anna Miroshnichenko
Anna Miroshnichenko

Reputation: 1213

RegExp with included and excluded symbols

I need to validate user input.

All English letters and symbols ".","-","&","'", "&" are allowed. Other symbols aren't allowed.

I have next RegExp which work for string "fffц":

var myRegExp = new RegExp("[a-z]+","ig")
myRegExp.test("fffц") // return false

But it doesn't work with string "fffцfff":

var myRegExp = new RegExp("[a-z]+","ig")
myRegExp.test("fffцfff") // return true, but string contains Russian letters, I expected false

How to write the correct regular expression?

Upvotes: 0

Views: 56

Answers (2)

Explosion Pills
Explosion Pills

Reputation: 191799

You have [a-z]+ which means "match at least once alphabetic character anywhere." You need to be using anchors

^[a-z.& '-]+$

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174836

The below regex would allow only english alphabets,numbers and sepcial characters.

var myRegExp = new RegExp("[ -~]+","ig")

Upvotes: 0

Related Questions