Search code examples
javascriptreactjsregexsafari

What is the alternate of lookbehind regex in safari browser?


What is the alternate of lookbehind regex in safari browser ?

I am using one regix where I want exclude http

Here is my regex :

 

value: /^((\w+)(?<!http):\/\/).+$/,

This is working fine in chrome but failing safari. Any look around for this.


Solution

  • You can re-vamp the lookbehind into a lookahead:

    /^(?!http:)\w+:\/\/.+$/ 
    

    Or even /^(?!https?:)\w+:\/\/.+$/ to account for https.

    Details:

    • ^ - start of string
    • (?!http:) - no http: allowed at the start
    • \w+ - one or more word chars
    • :\/\/ - :// text
    • .+ - one or more chars other than line break chars as many as possible
    • $ - end of string (redundant here though).