1

I am trying to detect either a space, single quote or double quote

Note: this answer does not work for single quote (unless it's the very first character, I need to detect either in front of, inside of or after a string): HTML5 input pattern search for quote

I have tried pattern="[^'\x20\x22]+" and also pattern="[^\x20\x22\x27]+"

Could someone give me a regex that detects all of them please.

parti
  • 205
  • 3
  • 15
  • `[^a-z]` is not the start of the line or a-z, it means **don't** match a-z https://stackoverflow.com/questions/4105956/regex-does-not-contain-certain-characters – Isaac Nov 16 '17 at 02:50
  • Do you want to match the string that doesn't contain a space/single quote or double-quote? – Gurmanjot Singh Nov 16 '17 at 02:54
  • @Gurman I want to match if the string DOES contain any of the mentioned characters, either before, inside of or after - I think opposite of what you showed below – parti Nov 16 '17 at 03:01
  • 1
    @parti Updated the answer – Gurmanjot Singh Nov 16 '17 at 03:05

1 Answers1

1

To match the strings that contain a space or ' or ", use this regex:

^(?=.*['\x20\x22]).+$

Click for Demo

Explanation:

  • ^ - asserts the start of the string
  • (?=.*['\x20\x22]) - positive lookahead to detect the presence of either a space or a ' or a "
  • .+ - matches 1+ occurrences of any character but a newline character
  • $ - asserts the end of the string
Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
  • I see your demo works for single quote, but in my input box it doesn't match - I'm wondering if the app is ignoring it - can you think of another way to check for single quote that might get detected? – parti Nov 16 '17 at 03:29
  • Your code does work, I also had to do an encoding fix in my app, so that it could see the single quote - All good now - thanks for your help. – parti Nov 16 '17 at 20:01