Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the result of this regular expression in javascript is true?

alert(/[a-z]+/.test("4Nyth1n&_."));

why the output is true? doesn't it supposed to match just the alphabet from a to z (lower case)?

like image 253
Yoyo_c Avatar asked Dec 27 '22 18:12

Yoyo_c


1 Answers

test just looks for a match of a substring. In this case, the substring yth matches. If you want to match the whole string, insert ^ and $:

alert(/^[a-z]+$/.test("4Nyth1n&_."));
like image 200
phihag Avatar answered Jan 18 '23 22:01

phihag