javascript 正则表达式中间不包含单词

javascript regex not cointains word in middle

我想检查正则表达式中间不包含单词。 我的正则表达式代码如下

/con\/.+\/top/.test("con/bottom/pop/down/top")

在此正则表达式中,第一个和最后一个将是 "con" 和 "top",在 .+ 之间,它不应匹配 /pop/.

您可以在每个斜杠后使用前瞻:

const re = /con\/((?!pop\/)[^\/]+\/)+top/;
console.log(re.test("con/bottom/pop/down/top"))
console.log(re.test("con/bottom/popo/down/top"))
console.log(re.test("con/bottom/top"))
console.log(re.test("con/pop/top"))
console.log(re.test("con/popo/top"))