I have the following Regex:
var legal = /[a-zA-Z0-9\._\-]+/;
Which is supposed to define that the only valid strings are the ones made up of those characters inside []
, repeated a positive number of times.
And so, the following works fine:
legal.test("a"); // => true
legal.test("/"); // => false
However, this does not work fine:
legal.test("a/"); // => true
It should be false. I tried putting a $
at the end of the ReGeX to signify end of line, and it helps, but not always:
var legal2 = /[a-zA-Z0-9\._\-]+$/;
legal2.test("a"); // => true
legal2.test("a/"); // => false
legal2.test("a/a"); // => true
The last one should be false
, so the $
in the end doesn't always help.
What do I need to do to my Regex to make it act like a "template" for which strings to be validated?