0

please help I have the regex line ^((?![<>=^@#]).)*$ which checking not ordinary symbols in an input field, it's ok but I need to add for this line one more condition , my line need to have vs.

For example when we have the name of sport game like this Patriots vs. Tigers

How can I complete my ^((?![<>=^@#]).)*$ condition and add rule for checking vs. in line (input field must have vs.) ?

It will be so cool if conditional also check spaces around vs. at left and at right, because for example Patriotsvs.Tigers is not good and need to show error also

4
  • Why not /^\w+\s+vs\.\s+\w+$/ Commented Jan 18, 2016 at 13:43
  • If you want to complicate it further, try ^((?![<>=^@#]|\bvs\.\B).)*\bvs\.\B((?![<>=^@#]).)*$. Commented Jan 18, 2016 at 13:46
  • What do you mean by "vs."? Commented Jan 18, 2016 at 13:53
  • Also, as others have alluded to, (?![<>=^@#]). is equivalent to [^\r\n<>=^@#]. Commented Jan 18, 2016 at 13:54

2 Answers 2

1

I think what you want is

/^[^<>=^@#]*?\bvs\.[^<>=^@#]*$/

which blacklists the characters [<>=^@#] and requires the literal text "vs." somewhere in the string.

That character blacklist is probably insufficient if you're trying to only approve inputs that won't lead to SQL-injection or XSS. Please consider using a stock input filtering/escaping system with this.

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

2 Comments

Nice, but "Patriots vs.Tigers" or "Patriots vs..." would match, which I'm not sure is expected.
Yeah. You could try changing the second * to +, but I think this regex is already doing too much work. Using two regex passes to separate filtering from parsing would address that, and emptiness would be best checked in the second.
0

You can use look aheads with the start anchor to effectively use multiple conditions. Here is something that should work for you:

^(?=((?![<>=^@#]).)*$)(?=.*?\svs\.\s).*$

Will match:

thing vs. another
Patriots vs. Tigers

Won't match:

th%^hg vs. another
thing another
thingvs.another

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.