2

I have this regex: https://regex101.com/r/bM5sQ0/2

<input type="text" pattern="[\p{L}]+\s[\p{L}]+">

Which I try to match the following text patterns:

Firstname Surname
Fírstnámé Súrnámé

And not match these lines:

Firstname Surname000
Fírstnámé Súrnámé000

I want to solve this globally with defining every unicode letter (what if someone is not Hungarian, but Polish, German, French, or Spanish instead and I didn't include their special characters?). However my solution does not work.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Lanti
  • 2,299
  • 2
  • 36
  • 69
  • 1
    As far as I know, every strange character has a unicode code. So you have to be more specific than that. For now all I can see is `/^\D+$/`, i.e. everything except numbers. See here: https://regex101.com/r/zK0qA7/1 – Tamas Rev May 17 '16 at 15:39

1 Answers1

2

If you're using a browser that does support \p{}, and doesn't require the u switch to enable it, your code works, but you should remove the brackets because they're unnecessary:

<input type="text" pattern="\p{L}+\s\p{L}+">

It worked when I tested it in Chrome.

Older Javascript versions (before ES2018?) do not support \p{} at all, and some versions may need the u switch to enable it, which won't work here. If you really need it, I suggest that you try the solutions here: How can I use Unicode-aware regular expressions in JavaScript?.

If you just don't like digits, then you can use \D as tamas rev said in the comments. Or maybe [^\d\s] to enforce that your input isn't just spaces.

Note that only matching letters is a bad way to validate names, since it excludes names like "O'Henry". Note that forcing exactly one space to be present excludes languages where the names are not separated with a space (like in the name "蔡英文"), people who only have one name, and people whose names have more than one space ("Mary Jane", "van der Waals"). And some names do have numbers. See Falsehoods Programmers Believe About Names.

Laurel
  • 5,965
  • 14
  • 31
  • 57
  • is unicode regex enabled by default in the HTML `pattern` attribute? – oldboy Oct 07 '19 at 01:46
  • You are actually wrong. He's trying to use a unicode character class, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes It looks like the u flag is even enabled by default on pattern: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern – Lea Verou Jul 16 '22 at 12:21
  • @LeaVerou This answer was entirely correct when it was written, and was still correct for older versions of Javascript. Still, I rewrote it based on testing I did today. – Laurel Jul 16 '22 at 13:09