I am using this regular expression /(?<=^| )\d+(\.\d+)?(?=$| )/
which contains a positive lookbehind but it seems it’s not working in Firefox and I’m getting an exception. What is the alternative to this?
Asked
Active
Viewed 1,392 times
0

Sebastian Simon
- 18,263
- 7
- 55
- 75

Amit
- 27
- 3
-
3Lookbehinds [aren’t supported in Firefox](https://bugzilla.mozilla.org/show_bug.cgi?id=1225665) yet. Please [edit] your question and explain _how exactly this regex is used_ in context. – Sebastian Simon Mar 29 '19 at 04:31
-
Most likely, a word boundary (`\b`) would be a sufficient (but not perfectly equivalent) replacement for both lookarounds, e.g. `/\b\d(\.\d+)?\b/`. You might also try splitting the original string by spaces and parsing individual parts. – p.s.w.g Mar 29 '19 at 04:41
-
Lookbehinds are now supported since June 30, 2020 (see [release notes](https://www.mozilla.org/en-US/firefox/78.0/releasenotes/)) – Wiktor Stribiżew Jul 07 '20 at 07:39
1 Answers
0
You could turn the positive lookbehind in a non capturing (?:^| )
group keeping the alternation.
Then capture your value in a capturing group (\d+(?:\.\d+)?)
and turn the optional decimal part also into a non capturing group. The positive lookahead is supported so you can keep that as is.
(?:^| )(\d+(?:\.\d+)?)(?=$| )
let strings = [
"1",
"1.2 ",
"0",
"0.122",
" 1",
" 1.2",
" 0",
" 0.122",
];
let pattern = /(?:^| )(\d+(?:\.\d+)?)(?=$| )/;
strings.forEach(s => {
console.log(s.match(pattern)[1])
});

The fourth bird
- 154,723
- 16
- 55
- 70