2

I do have the following RegExp in the current web application.

function myCyrillicDigitsAndSpaceValidator(text){
    return XRegExp("^[\\p{Cyrillic}0-9]+").test(text);
}

As you can see I use XRegExp javasciprt library. Currently, this regxep checks if its cyrillic+has space+has numbers. I want to extend it and to check:

It's Cyrillic

It does have space

It does have numbers

It does have special chars

XregxExp version is 2.0.0 if it does matter

Correct examples:

1 Май

-

Май 25\5
Emma
  • 27,428
  • 11
  • 44
  • 69
Daler
  • 1,205
  • 3
  • 18
  • 39

2 Answers2

0

You can simply add any special chars and other chars that you wish to the list you already have, such as:

^[\\p{Cyrillic}0-9#$%^&\-\s\\]+$

or

([\\p{Cyrillic}0-9\\s\-#$%^&\\]+)

DEMO

enter image description here

RegEx

If this expression wasn't desired, it can be modified or changed in regex101.com.

RegEx Circuit

jex.im also helps to visualize the expressions.

enter image description here

Emma
  • 27,428
  • 11
  • 44
  • 69
  • It's good practice to either move the `-` after `\s` to the end or beginning or to escape it. Then there is definitely no chance of it being interpreted as a range. – VLAZ May 21 '19 at 05:33
  • This doesn't seem correct, because e.g. it would match a purely numeric text, or a text which only had e.g. Cyrillic characters and nothing else. – Tim Biegeleisen May 21 '19 at 05:34
  • it gives error: `Uncaught SyntaxError: Invalid regular expression: /^[\u0400-\u0484\u0487-\u0527\u1D2B\u1D78\u2DE0-\u2DFF\uA640-\uA697\uA69F0-9#$%^&-s\]+/: Unterminated character class` – Daler May 21 '19 at 05:43
  • For some reason it's working fine in validator demo but not in actual app. This is the new error `Uncaught SyntaxError: Invalid regular expression: /[p{Cyrillic}0-9s-#$%^&\]+/: Range out of order in character class` – Daler May 21 '19 at 06:10
  • 1
    But in my OP it's working just fine without any escaping... why should I escape meta chars now? – Daler May 21 '19 at 06:16
0

You may try using positive lookaheads for this purpose. Consider the following regex pattern:

^(?=.* )
 (?=.*\d)
 (?=.*[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/])
 (?=.*[\p{Cyrillic}])
 [ \d-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/\p{Cyrillic}]+
$

This asserts, in order, that a space, number, symbol, and Cyrillic character all occur in the input. Then, it matches one or more of these four classes of characters.

Your updated code:

function myCyrillicDigitsAndSpaceValidator(text){
    return XRegExp("^(?=.* )(?=.*\\d)(?=.*[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\/])(?=.*[\\p{Cyrillic}])[ \\d-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\/\\p{Cyrillic}]+$").test(text);
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • @Daler I updated my answer, and it seems to at least parse correctly now ([see here](https://rextester.com/XFJMT57244)). Try it again please. – Tim Biegeleisen May 21 '19 at 05:49
  • tests doesnt pass with this regexp. The result is always false for tested strings – Daler May 21 '19 at 05:51
  • I had the same result, but I was using `RegExp`. Not sure if perhaps you have some encoding problem on your end. – Tim Biegeleisen May 21 '19 at 05:52
  • my original expression in my post does work just fine. It doesn't have any encoding problems.. Must be something wrong with expression itself – Daler May 21 '19 at 05:56
  • Minus the Cyrillic issue, my `XRegExp` code is basically working for me here locally. I will tell that lookaheads are the way to go in this case, most likely. – Tim Biegeleisen May 21 '19 at 06:06
  • it still doesnt work.. acts the same way as it was before not validating anyhting – Daler May 21 '19 at 06:07
  • I can't debug any more than I already have. If you remove the restrictions on Cyrillic, the pattern works. Pick this up and go from here. – Tim Biegeleisen May 21 '19 at 06:08
  • Thanks for your effort. But the `Cyrillic` is the main part. I can't remove it. Otherwise I would simply remove any sort of validation on that field.. – Daler May 21 '19 at 06:11