Skip to content
Advertisement

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

Advertisement

Answer

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 (?!Bw) 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.

Advertisement