1

I want to check regex not contains word in middle. my regex code below

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

in this regex first and last will be "con" and "top", in between .+ it should not match /pop/.

Selvakumar
  • 527
  • 2
  • 13

1 Answers1

1

You could use lookahead after each slash:

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"))
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320