abhayance
abhayance

Reputation: 13

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 + ")"
    ));

Upvotes: 1

Views: 139

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

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.

Upvotes: 1

Related Questions