3

A regex is needed in order to exclude sentences with wag[.|on] or ut[i|e] but only if NO sed[.|an] is present and allow all other sentences. any suggestions?
ie. exclude matches that are only wagon or only ute.

I tried /[^wag[on|.]]/ig.test(sentence) but this way will not allow exclusion. I need to select the "yes" sentences only as below.

Given the following sentences:
the sedan is fast <-- yes
other sed. is fast too <-- yes
the wagon is slow <-- no
other wag. is also slow <-- no
the ute is slow <-- no
other uti is also slow <-- no
the wag. and wagon slower then sed. or sedan <-- yes
the uti or ute is slower then sed. or sedan <-- yes
both wag. wagon and uti and ute are slow <-- no
nothing is fast or slow <-- yes

3
  • have you considered to use two regex for this? I don't think you can make it one. Commented Jun 2, 2017 at 3:44
  • could you give some solution as to how you could do it with 2 regex. thx Commented Jun 2, 2017 at 3:56
  • do you mean wag[.|on] or wag(on)? or wag(.|on)? they are different. Commented Jun 2, 2017 at 5:19

2 Answers 2

2

this does the 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

Sign up to request clarification or add additional context in comments.

Comments

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

you can use (?!) to match a string who do not match some pattern.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.