There is a task to write a regular expression to skip 2 words, for example, the name and surname, in any language (English, German ...).

That's how i thought

var str = "Имя фамилия тСкст тСкст"; str.match(/^[^!@#]+ [^!@#]+$/i); 

And here is the result

 ["Имя фамилия тСкст тСкст"] 

Why is the regular expression validated? Maybe I do not check correctly, I do not understand ...

In theory, the regulars should find a match.

start_string [any_symbol_krome! @ #] minimum 1 time space [any_symbol_krome! @ #] end_string

But, in the example after the 2nd word there are still spaces, characters, and they also miss

I need to check for any characters except those 3 characters.

Here is the tester https://regex101.com/r/kJ5vZ2/4

  • one
    even if you leave it like this ^[^!@#]+$ will also pass :) you do not include a space in the forbidden characters, add it to the brackets it will work - splash58

1 answer 1

You yourself answer your question. The regular expression [^!@#]+ Includes any characters except ! , @ and # including a space ! And this means that the entire string Имя фамилия тСкст тСкст fits the first [^!@#]+ . Add a space to the list of excluded words.

Example:

 var str = "Имя фамилия тСкст тСкст"; str.match(/^[^!@# ]+ [^!@# ]+$/i); 

https://regex101.com/r/kJ5vZ2/5