Search code examples
regexsafaripositive-lookbehind

Alternative to positive lookbehind regex


I am working with Prism.js for syntax highlighting and I have a regex for detecting Kotlin infix function (?<=\w\s)(\w+)(?=\s\w) (https://regex101.com/r/wVSO6G/1) which uses a positive look ahead and a positive look behind, however this does not work on Safari browsers which breaks my entire website.
I have seen alternatives that involve using the matcher but I don't have that option as I can only provide the regex, for example:

"infix": [
  {
    pattern: /(?<=\w\s)(\w+)(?=\s\w)/,
  },
],

Is there an alternative to this that would work on Safari?


Solution

  • If you can only provide a regex and the whole match value is highlighted, there is no way to mimic the current lookbehind pattern. The best you could think of is a regex like /\b\s(\w+)(?=\s\w)/ that would also highlight the leading whitespace (unless there is an option to highlight only some group value).

    If you can tweak the inner code, you could use a group replacement: find /(\w\s)(\w+)(?=\s\w)/ and replace with $1<span class="highlight">$2</span>.