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
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).
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'))
})