Javascript regex character exception

I have a js regex like this:

/^[a-zA-ZęóąśłżźćńĘÓĄŚŁŻŹĆŃ]+$/ 

and now I would like to exclude a-zA-Z from letters such as QVXqvx. How to change the syntax of this regular expression?

I tried but no luck. Please help me.

Thanks in advance

+4
source share
4 answers

You can still do ranges, but you have to do ranges that exclude these letters, so something like A-PR-UWYZ

+7
source

The best way to do this is to simply update the range to exclude letters that you do not want. That would leave you with this:

 /^[a-pr-uwyzA-PR-UWYZęóąśłżźćńĘÓĄŚŁŻŹĆŃ]+$/ 

You can cross out the character class subtraction form with a negative view. However, it will be less efficient since you are repeating a negative result for each row. In any case, here's what it would look like:

 /^(?:(?![qvxQVX])[a-zA-ZęóąśłżźćńĘÓĄŚŁŻŹĆŃ])+$/ 

This works best if you don't repeat the character class an unlimited number of times.

Several varieties of regular expressions, including Java and .NET, efficiently support character class subtraction using special syntax.

In Java, intersect with a negative group:

 /^[a-zA-ZęóąśłżźćńĘÓĄŚŁŻŹĆŃ&&[^qvxQVX]]+$/ 

A little-known fact is that the Opera web browser actually supports the above Java syntax in its own JavaScript regular expressions. Opera may remove this feature in the future because it is non-standard (it is based on abandoned ES4 offers), but it works in the current version (v11.64), at least.

.NET, XPath, and XML Schema support the following, simpler syntax for subtracting a character class:

 /^[a-zA-ZęóąśłżźćńĘÓĄŚŁŻŹĆŃ-[qvxQVX]]+$/ 
+7
source

You can not. In this case, you need to manually list all the letters except the excluded QVXqvx

+1
source

/^[a-pA-PR-Ur-uWwYyZzęóąśłżźćńĘÓĄŚŁŻŹĆŃ]+$/

0
source

Source: https://habr.com/ru/post/1414642/


All Articles