0

Is there a fix/callback for browsers that do not support lookbehind?

The only thing I found was a way to detect whether lookbehind is supported or not, but doesn't give an alternative. (See code below)

I'm looking for something that detects whether a lookbehind is entered in a input box and provides an alternative. Even better still when the lookbehind is handled in the background.

I know regex101 is able to handle lookbehinds on older browsers but I wasn't able to figure out how it's done.

An example lookbehind is as follows: (?<!\n)- This one matches a dash of not at the beginning of a string. (The string is multined in a textarea)

var isLookBehindSupported = false;
try {
    isLookBehindSupported = !!new RegExp("(?<=)");
} catch (e) {
/*In unsupported browsers, trying to create a lookbehind expression will simply error, which is caught here*/ 
} 
if (isLookBehindSupported) { 
   // Yay, lookbehind expressions are supported 
} 
else { 
   // Booo! Lookbehind not supported 
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
MK01111000
  • 770
  • 2
  • 9
  • 16
  • 4
    If you can give an example regex which uses a lookbehind, then there is likely a chance that it could be rewritten without using lookbehinds. Even regex engines which support lookbehind do not all necessarily support variable width lookbehinds. But in general, to ask for code which can refactor an arbitrary regex to remove lookbehinds might be too broad or not even possible. – Tim Biegeleisen Nov 29 '18 at 13:52
  • @Tim, I updated my question – MK01111000 Nov 29 '18 at 13:57
  • 1
    Any reason you cannot use capture instead of lookbehind? The example regexp can easily be implemented without lookbehind - `[^\n](-)` then the match is simply `result[1]` – slebetman Nov 29 '18 at 13:59
  • Your edit somewhat misses the point of Tims comment. Give a real example of a Regex you want to use, not how you detect if look behind is supported. Bear in mind that even when look behinds are supported their performance is typically sub optimal and should be avoided. Basically if you can write something without a look behind (which most of the time you can) don't use a look behind at all – Liam Nov 29 '18 at 14:01

0 Answers0