问题
I'm using regex pattern matching for HTML5 form validation. The latest version of Firefox gives me an error. I only started seeing this in Firefox 46. I don't think this was a problem in earlier Firefox versions.
Unable to check
<input pattern='[\@\%]'>
because the pattern is not a valid regexp: invalid identity escape in regular expression
Caused by this very simple test case:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<form>
<input pattern="[\@\%]">
</form>
</html>
Why is escaping these characters considered an error? I've always escaped everything in my regular expressions that isn't a number or a letter. I've never had anything complain this type of escaped character except this version of Firefox.
When I learned regex, I was told that everything that wasn't a number or a letter could have special meaning. Even if it doesn't now, it might in a future version, so it is better to escape them. Is this not true?
Is there a list of characters I shouldn't escape for Firefox?
回答1:
This is due to the following change: Bug 1227906 - HTML pattern attribute should set u flag for regular expressions
As someone has already said, you don't have to escape those characters. Just use:
<input pattern="[@%]">
来源:https://stackoverflow.com/questions/36953775/firefox-error-unable-to-check-input-because-the-pattern-is-not-a-valid-regexp