我想检查正则表达式中间不包含单词。 下面是我的正则表达式代码
/con\/.+\/top/.test("con/bottom/pop/down/top")
在此正则表达式中,first 和 last 将是“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"))
关于javascript正则表达式不包含中间的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50094261/