2

I have a good RegExp which does not allow numbers at all:

/^[\D]*$/

Now I just need that it also will not be able to prevent special chars like:

!@#$%^&*()_+-/."'<>\|±§`

and etc (if there more special chars which I don't know - I will be glad to block them too).

This does work - but it's probably won't cover all cases:

/^[^\d^!^@^#^$^%^&^*^(^)^[^\]^{^}^;^:^|^,^<^>^.^?^/^\\^~^`^±^§]*$/
Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96
  • 1
    Might be easier to figure out which characters you *do* want to allow, if the set is reasonable. Also note there's no point in using a character set with only a single token, just use that token instead – CertainPerformance Oct 18 '18 at 05:41
  • Alphabeta - on all languages. I know I can use `[a-z]/gi` but if user types in NON english it will be blocked. – Raz Buchnik Oct 18 '18 at 05:42
  • You might find the answer you are looking for here : https://stackoverflow.com/a/50060158/2928853 – jrook Oct 18 '18 at 05:52
  • 1
    FWIW, `[^\d^!^@^#^$^%^&^*^(^)^[^\]^{^}^;^:^|^,^<^>^.^?^/^\\^~^\`^±^§]` is equivalent to `[^\d^!@#$%&*()[\]{};:|,<>.?/\\~\`±§]`. `^` only has a special meaning at the beginning of the character class. Otherwise it just represents the character itself, so repeating it is unncessary. – Felix Kling Oct 18 '18 at 06:41
  • @FelixKling what you said prevents `!@#` but not prevents `a!@#` – Raz Buchnik Oct 18 '18 at 06:44
  • 1
    `/^[^\d^!@#$%&*()[\]{};:|,<>.?/\\~\`±§]*$/.test('a!@#')` -> `false`, but anyways, I was just commenting on the syntax of your expression, not providing a solution to your problem. – Felix Kling Oct 18 '18 at 06:48
  • Maybe it didn't work for me since I am using `Joi` and it does not using the `.test()` method but something else? (Just to be sure) – Raz Buchnik Oct 18 '18 at 06:50
  • Try https://regex101.com/r/UR0ObC/4 or, if you also need to support punctuation chars from the astral plane, https://regex101.com/r/UR0ObC/5 – Wiktor Stribiżew Oct 18 '18 at 09:24

1 Answers1

2

You need to use a version of Javascript that supports regex with full Unicode (i.e. ES 6+). Then you can use a regex like this:

/^\p{L}*$/gu

This only allows characters present in the L unicode character class, which stands for "Letters".

var regex = /^\p{L}*$/gu;
console.log("abc".match(regex));
console.log("αβγ".match(regex));
console.log("абв".match(regex));
console.log("ひらがな".match(regex));
console.log("中文".match(regex));
console.log("!@#$".match(regex));
console.log("1234abc".match(regex));
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • I am using this using the `Joi.validate()` on Node.js. And I receive the following error: `SyntaxError: Invalid regular expression: /^\p{L}*$/: Invalid escape` and this is what I have enetered: `last_name: Joi.string().min(2).max(10).regex(/^\p{L}*$/gu)` – Raz Buchnik Oct 18 '18 at 06:15
  • 1
    @Raz Then you are using a version of Javascript that does not support Unicode matches. You need to use ES 6, or find a workaround as described [here](https://stackoverflow.com/a/280762/5133585) – Sweeper Oct 18 '18 at 06:18
  • @Raz Can you check what version you are using? Also, does the snippet work for you? – Sweeper Oct 18 '18 at 06:20
  • Node v is `v9.2.0` and as I said it returns with an error. The only thing I found that works is something I made manually: `/^[^\d^!^@^#^$^%^&^*^(^)^[^\]^{^}^;^:^|^,^<^>^.^?^/^\\^~^`^±^§]*$/` But I cannot be sure that I covered all the "special" chars – Raz Buchnik Oct 18 '18 at 06:24
  • This is also good but not allowing non english: `/^[^\d^\W]*$/` – Raz Buchnik Oct 18 '18 at 06:31