Home > Net >  At least 3 letters for all languages with whitespaces
At least 3 letters for all languages with whitespaces

Time:01-03

I need a regex which recognize at least 3 letters from whole string for all languages.

I tried

/\p{L}{3}/u

But its not working for name Wu ko

I need something like this:

test string expected result
t me true
wu ko true
Ker le true
john doe true
f i false
fe false

CodePudding user response:

try this regex: /(\p{L}.*?){3}/u As there could be other characters in between the letters(of any language), we can use the . to match all characters. The *? allows us to keep matching until we find another \p character

CodePudding user response:

You may check if there are any three letters anywhere inside a string using

const regex = /(?:\P{L}*\p{L}){3}/u;

If you only want to allow whitespaces and letters, you will need to precise the pattern to

const regex = /^(?:\s*\p{L}){3}[\p{L}\s]*$/u;

See the regex demo.

Details:

  • ^ - start of string
  • (?:\s*\p{L}){3} - three occurrences of zero or more whitespaces followed with a letter
  • [\p{L}\s]* - zero or more letters or whitespaces
  • $ - end of string.
  • Related