简体   繁体   中英

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?

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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM