Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Regex中的消极前瞻似乎不起作用_Node.js_Regex_Negative Lookahead - Fatal编程技术网

Node.js Regex中的消极前瞻似乎不起作用

Node.js Regex中的消极前瞻似乎不起作用,node.js,regex,negative-lookahead,Node.js,Regex,Negative Lookahead,我试图使用正则表达式从遵循严格模式的url中提取子域。我只想将URL与指定的子域相匹配,所以我使用的是否定的前瞻。这似乎在许多正则表达式求值器中都有效,但当我在节点中运行时,两个字符串都会匹配。代码如下: const defaultDomain = 'https://xyz.domain.com'; const scopedDomain = 'https://xyz.subdomain.domain.com'; const regex = /^https:\/\/xyz\.([^.]+(?!d

我试图使用正则表达式从遵循严格模式的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为空,matchPrefix2导致第一个捕获组为“子域”的匹配

实际:matchPrefix1和matchPrefix2都包含数据,捕获组分别返回为“域”和“子域”

链接到regexr works!:

指向repl的链接不起作用:


这里发生了什么事?

Regexr显示您的代码正在工作,因为您没有添加多行标志。这会导致第二行的开头与^不匹配,因此将忽略整个第二行。添加多行标志以查看您的正则表达式不工作

我会使用这个正则表达式:

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

我所做的更改是在检查后将[^.]+部分移动到?!领域基本上,你应该检查一下?!匹配xyz \..

后立即进入域,这太快了!非常感谢。