简体   繁体   中英

How to match all strings with a specific REGEX pattern that do not start with a defined character without using a negative lookbehind

I currently have a usecase on which I want to match all http:// and https:// strings in a text but only when they do not start with a " or ' using JavaScript. If they start with another character, eg, a whitespace, I still only want to match the http:// or https:// without the preceding character.

My current regex uses a negative lookbehind but I just realized that this is not supported in Safari:

/(?<!["'])(https?:\/\/)/gm

So what would be an alternative for using a negative lookbehind to match the following strings in a text:

  • http:// -> should match http://
  • https:// -> should match https://
  • xhttps:// -> should match https:// whereby x can be any character except " and '
  • "https:// -> should NOT match at all

No need of lookbebind here, use character class and groups:

 const vars = ['http://', 'https://', 'xhttps://', '"https://'] const re = /(?:[^'"]|^)(https?:\/\/)/ vars.forEach(x => console.log(x, '- >', (x.match(re) || ['',''])[1]) )

Regex :

(?:[^'"]|^)(https?:\/\/)

EXPLANATION

--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    [^'"]                    any character except: ''', '"'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    ^                        the beginning of the string
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    http                     'http'
--------------------------------------------------------------------------------
    s?                       's' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
  )                        end of \1

You should use this regex: /^[az]+:\/\//gm

Example :

const pattern = /^[a-z]+:\/\//gm
const string = `http://
https://
xhttp://
"http://
'https://`
console.log(string.match(pattern));
// Output: [ 'http://', 'https://', 'xhttp://' ]

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