Closed
Bug 1425763
Opened 7 years ago
Closed 7 years ago
Regex doesn't compile (but does in Chrome)
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
RESOLVED
INVALID
People
(Reporter: ianopolous, Unassigned)
References
Details
User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/63.0.3239.84 Chrome/63.0.3239.84 Safari/537.36
Steps to reproduce:
Load any js with the following code:
var regex = /^(?=.{1,32}$)(?![_-])(?!.*[_-]{2})[a-z0-9_-]+(?![_-])$/;
Actual results:
SyntaxError: invalid regexp group
Expected results:
It should parse fine. (The same regex works fine in Chrome, and in other languages like Java)
It is used to test for valid usernames on our site.
Comment 1•7 years ago
|
||
Pretty sure script regexes are a js engine thing.
Component: DOM: Core & HTML → JavaScript Engine
Comment 2•7 years ago
|
||
I just tried this in current Nightly and stable versions: both accept the regex without any issues. ianopolous, can you provide a more complete test case that directly reproduces the problem?
Flags: needinfo?(ianopolous)
Reporter | ||
Comment 3•7 years ago
|
||
It seems like either I copy pasted the regex wrong or something modified it, the correct regex is (it was missing the open angle bracket 8 from the end):
var regex = /^(?=.{1,32}$)(?![_-])(?!.*[_-]{2})[a-z0-9_-]+(?<![_-])$/;
simply pasting that line into the JS console on any page is sufficient to reproduce it,
or you can see it at:
https://ianopolous.github.io/FirefoxBug/
Flags: needinfo?(ianopolous)
Comment 4•7 years ago
|
||
Thanks, I can reproduce the issue with that regex. However, it's largely accidental that it works in Chrome. Chrome's JS engine recently added support for named regexp capture groups[1], which start with "(?<". Your expression doesn't contain a named capture group, however, because it doesn't contain the closing angle bracket, nor a name. I don't know exactly, but my guess is that Chrome simply ignores the angle bracket and treats this as a negative lookahead. Alternatively, it might treat it as a named group without a name? I'm not sure.
Note that this regexp is rejected not only by Firefox but also by at least Safari; I don't know about Edge. My recommendation is to change it, which might be as simple as removing the angle bracket. Should this really function as a named capture group in Chrome, please be aware that this is a very new feature and will not work for a large fraction of your users, including those on older versions of Chrome.
Given the above explanation, I'll close this as invalid, but please feel free to reopen if my analysis is incorrect.
[1] https://github.com/tc39/proposal-regexp-named-groups
Status: UNCONFIRMED → RESOLVED
Closed: 7 years ago
Resolution: --- → INVALID
Comment 5•7 years ago
|
||
Lookbehind assertions (bug 1225665) aren't yet supported in Firefox.
Depends on: 1225665
Reporter | ||
Comment 6•7 years ago
|
||
Thank you for your help, Till. I think the regex is correct and it isn't a named capture group. I've also tried changing it to remove the < but haven't been able to get it to work to spec (commonly "ted_" is accepted rather than failing).
According to the following it is a negative lookbehind:
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
(?<!X) X, via zero-width negative lookbehind
[Edit] just noticed the concurrent comment from Andre. Thanks for the reference!
Comment 7•7 years ago
|
||
(In reply to ianopolous from comment #6)
> Thank you for your help, Till. I think the regex is correct and it isn't a
> named capture group. I've also tried changing it to remove the < but haven't
> been able to get it to work to spec (commonly "ted_" is accepted rather than
> failing).
If I interpreted the original regular expression correctly, this one should be equivalent:
/^[a-z0-9](?:[a-z0-9]|[_-](?=[a-z0-9])){0,31}$/
A matching string must have:
- 1-32 characters
- start with [a-z0-9]
- and any following characters must be either [a-z0-9] or [_-], but if it is [_-], the next character must be [a-z0-9].
Reporter | ||
Comment 8•7 years ago
|
||
Thanks so much Andre! That's great and does the trick perfectly I think.
You need to log in
before you can comment on or make changes to this bug.
Description
•