'Converting a RegExp constructor to be Safari-compatible
I have this regexp constructed pattern I'm passing a string variable to. This works perfectly in Chrome, but won't work in Safari.
Is there a way I might be able to convert this code to be compatible across browsers? Thank you!
(e = e
.split(new RegExp("(?<!\\w)" + t[l] + "(?!\\w)(?![^\\[\\]]*\\])", "gm"))
.join(n)),
1 == caseinsensitive &&
(e = e.replace(
new RegExp("(?<!\\w)" + t[l] + "(?!\\w)(?![^\\[\\]]*\\])", "gmi"),
"[$&](" + n + ")"
));
Solution 1:[1]
You can use
(e = e
.split(new RegExp("(?!\\B\\w)" + t[l] + "(?!\\w)(?![^\\][]*])", "g"))
.join(n)),
1 == caseinsensitive &&
(e = e.replace(
new RegExp("(?!\\B\\w)" + t[l] + "(?!\\w)(?![^\\][]*])", "gi"),
"[$&](" + n + ")"
));
The (?!\B\w) negative lookahead requires a word boundary position if the next char is a word char. Else, if the next char is not a word char, no word boundary is required.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wiktor Stribiżew |
