简体   繁体   中英

Negative Look-ahead in Regex doesn't seem to be working

I'm attempting to use Regex to extract a sub-domain from a url that follows a strict pattern. I want to only match urls with subdomains specified , so I'm using a negative look-ahead. This seems to work in many regex evaluators, but when I run in node, both strings get matched. Here's the code:

const defaultDomain = 'https://xyz.domain.com';
const scopedDomain = 'https://xyz.subdomain.domain.com';

const regex = /^https:\/\/xyz\.([^.]+(?!domain))\./
const matchPrefix1 = defaultDomain.match(regex);
const matchPrefix2 = scopedDomain.match(regex);

console.log(matchPrefix1);
console.log(matchPrefix2);

Expected: matchPrefix1 is null and matchPrefix2 results in a match where the first capture group is 'subdomain'

Actual: both matchPrefix1 and matchPrefix2 contain data, with the capture groups coming back as 'domain' and 'subdomain' respectively

Link to regexr (works!): https://regexr.com/42bfn

Link to repl (does not work): https://repl.it/@tomismore/SpiffyFrivolousLaws

What's going on here?

Regexr shows your code working because you didn't add the multiline flag. This causes the start of the second line to not match ^ , so the whole second line is ignored. Add the multiline flag to see your regex not working.

I would use this regex:

^https:\/\/xyz\.(?!domain)([^.]+)\.

The change I made is to move the [^.]+ part to after checking (?!domain) . Basically, you should check for (?!domain) immediately after matching xyz\\. .

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