【发布时间】:2019-04-05 19:59:34
【问题描述】:
我正在尝试使用 Regex 从遵循严格模式的 url 中提取子域。我只想匹配 与指定的子域 的 url,所以我使用的是否定的前瞻。这似乎适用于许多正则表达式评估器,但是当我在节点中运行时,两个字符串都会匹配。代码如下:
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);
预期: matchPrefix1 是 null,而 matchPrefix2 会导致第一个捕获组是“子域”的匹配项
实际: matchPrefix1 和 matchPrefix2 都包含数据,捕获组分别返回为“域”和“子域”
链接到正则表达式(有效!):https://regexr.com/42bfn
repl 链接(无效):https://repl.it/@tomismore/SpiffyFrivolousLaws
这是怎么回事?
【问题讨论】:
标签: node.js regex negative-lookahead