Reputation: 1677
When I test my javascript at this site it behaves as I would expect.
However when I try and test it on my page it ALWAYS fails the test
function testName() {
if (new RegExp('^(?!^(\..+)?$)[^\x00-\x1f\\?*:^&!`~@#$$+=<>\?\*;|/]+$').test("me")) {
alert("good");
}
else {
alert("invalid characters");
} return false;
}
The expression is supposed to test a file name for special characters like ^&*!~
+=<>` etc.
Am I missing something stupid?
Upvotes: 0
Views: 556
Reputation: 536755
Aside from the escaping issue, lookahead assertions (your (?!
group) are simply broken in IE (and weren't in the original JavaScript spec). They should not be used in client-side JavaScript. Instead, break the tests in that clause out into separate if conditions.
(If this is a filename tester, you shouldn't rely on it for security as there are many broken constructs it does not catch.)
Upvotes: 1
Reputation: 1677
Once I closed my browser and cleared the cache, my original expression ended up working, which is good because like Warren young said the substitute expression was ugly.
if(/^[\w-. ()\']+$/.test(this.form.mediaFile.value)) return true;
else return false;
Upvotes: 0
Reputation: 414006
When you put your regex into a string (like when you use "new RegEx()" instead of /.../ notation), the regex string has to change. Why? Because it's going to be parsed twice now instead of once: first, as a string - which means all the normal backslash processing that happens inside string constants will take place, and second, as a regex.
Upvotes: 6
Reputation: 42383
Not wanting to parse your huge ugly RE, I can see one of two problems:
Your test is backwards. The test() call will return false
in your example above, meaning that it didn't match any bad characters in "me", so you will get an alert saying "invalid characters", which is backwards.
Your syntax for the test is wrong. Try using a regex literal instead:
if (/^(?!^(\..+)?$)[^\x00-\x1f\\?*:^&!`~@#$$+=<>\?\*;|/]+$/.test("me")) {
Upvotes: 1