2

I have an string of |DOGS|MAN|CAT| and I need to check that DOG and CAT exist in the string.

The input string is always split between pipes.

I have this at present:

((^|, )(?=.*\|DOG|\b.$)(?=.*\|CAT\b).*$)

This almost works, if my input string was |DOG|MAN|CAT| it is fine but as I have specified DOGS not DOG it should not match

4
  • Is your input string always pipe-delimited? I'm not entirely clear on what your possible inputs are. Commented Apr 21, 2019 at 17:28
  • 1
    if (/^(?=.*\|CAT\|)(?=.*\|DOG\|)/.test("|" + s + "|") { return true; } Commented Apr 21, 2019 at 19:12
  • yes, the input is always pipe separated. Commented Apr 22, 2019 at 8:36
  • Wiktor, this works exactly as intended, thank you Commented Apr 22, 2019 at 8:37

1 Answer 1

5

This RegEx might help you to create a group, where you can list all your desired target string in it using a | (OR), and you may not bound it from left and right, if possible:

\b(DOG|CAT|ANYTHING|ELSE|THAT|YOU|WISH)\b

RegEX

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

1 Comment

The first one works if needing to match either DOG or CAT which is another thing i need to do. the second doesn't fit my needs as it matches CAT when in the pipes it is CATS. Thank you though, the first is usefule

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.