Javascript regex to exclude pattern only if other pattern doesn't match

A regex is required to exclude clauses with wag[.|on]

or ut[i|e]

, but only if NO is sed[.|an]

present and allows all other clauses. any suggestions?
i.e. exclude matches that are only wagons or only ute.

I've tried /[^wag[on|.]]/ig.test(sentence)

but this path won't allow exclusion. I only need to select the yes sentences as shown below.

Considering the following suggestions:
sedan fast - <- yes another sed. also fast - <- yes wagon slow <- no
other wag. also slow <- no
ute slower <- no
other uti also slow <- no
vag. and the carriage is slower then sed. or sedan - yes uti or ute is slower than sed. or a sedan - yes, both wag. wagon, uti and ute are slow. nothing fast or slow <- yes

+3


source to share


2 answers


this is a trick

function isMatch(input) {
   var regno = /(wag[.|on]|ut[i|e])/gi;
   var regyes = /sed[.|an]/gi;
   return !regno.test(input) || regyes.test(input);
}

      



result:

enter image description here

+2


source


/^(?:.*sed[.|an].*|(?:(?!wag[.|on]|ut[i|e]).)*)$/

      



you can use (?!)

to match a string that doesn't match some pattern.

0


source







All Articles