I'm using a regex to test the validity of a name. This is the code:
if (!/^[A-Za-zÀ-Ÿ- ]+$/.test(value)) {
doSomething()
}
Unfortunately in the browser I get this error:
Uncaught SyntaxError: Invalid regular expression: /^[A-Za-zÀ-ȕ- ]+$/: Range out of order in character class.
The minified js has the code correctly compiled, but I load this .js file in .jsp file on my server. How does this influence the final messed up expression that I ended up with?
It appears as though the browser is interpreting the special characters in a character set other than utf-8. Make sure the web page is set to use utf-8 encoding.
<meta charset="utf-8">
Another idea to help with the regular expressions is to use unicode escaping. This may help with the encoding issue regardless of encoding.
You could replace the regex as such (assuming I have the character codes correct):
if (!/^[A-Za-z\u{c380}-\u{c5b8}- ]+$/.test(value)) {
doSomething();
}
And if you wanted to check for - in your expression, probably escape it as well (\-).
if (!/^[A-Za-z\u{c380}-\u{c5b8}\- ]+$/.test(value)) {
doSomething();
}