Regex - starts with a particular string but does not end with another substring

后端 未结 2 938
你的背包
你的背包 2021-01-24 04:23

Given two strings s1 and s2, I am trying to write a regex that will match a string that starts with s1 but does not end with s2

相关标签:
2条回答
  • 2021-01-24 05:09

    Try this:

    /^(?!.+BAD$)TEST-.*/
    

    This matches the start, goes ahead and rejects anything ending in the bad string, then matches the desired pattern.

    Here's a demo that passes all four of your tests (click "RUN TESTS" at the bottom to verify).

    0 讨论(0)
  • 2021-01-24 05:24

    You can use startsWith and endsWith

    let arr = [`TEST-101`, `TEST-SOME-DESC`,`TEST-101-BAD`,`TEST-SOME-DESC-BAD`]
    
    arr.forEach(e=>{
      console.log(e, e.startsWith('TEST') && !e.endsWith('BAD'))
    })

    0 讨论(0)
提交回复
热议问题