3

I have written a regular expression for Name validation ^[a-zA-Z\\s]+$ in English language and now I need it to support Japanese language too. So how to write regular expression validation in JavaScript.

MERLIN THOMAS
  • 747
  • 1
  • 7
  • 15
  • Validate the name of what? –  Apr 07 '18 at 05:29
  • First Name and Last Name of a person – MERLIN THOMAS Apr 07 '18 at 05:30
  • 1
    What is the point of this? What are you trying to exclude? Whatever regexp you come up with will yield false positives (says it's a name, but actually it isn't), and false negatives (says it's not a name, but actually it is), leaving somebody very frustrated. –  Apr 07 '18 at 12:10
  • I am trying to exclude special characters and digits in name. And validate name is in English or Japanese Language. – MERLIN THOMAS Apr 07 '18 at 13:55
  • Does this answer your question? [Use regular expression to match ANY Chinese character in utf-8 encoding](https://stackoverflow.com/questions/9576384/use-regular-expression-to-match-any-chinese-character-in-utf-8-encoding) – Andrea Ciccotta Jan 19 '21 at 20:59

1 Answers1

0

Assuming we expect unicode. To match against range use this form of regex [\u{hhhh}-\u{hhhh}] . Find the appropriate range you want to support you can use this page for reference. Finally don't forget to enable unicode flag /$[\u{hhhh}-\u{hhhh}]+^/u. Here is an example which will match against a word consisting of only katakana characters

/^[\u{30a0}-\u{30ff}]+$/u

Or use this range to include japanese punctuation.

/^\u[{3000}-\u{303f}\u{30a0}-\u{30ff}]+$/u

Complete example

console.log("Merlin Thomas".match(/^[a-zA-Z \u{30a0}-\u{30ff}\s]+$/u))
console.log("メルリントーマス".match(/^[a-zA-Z \u{30a0}-\u{30ff}\s]+$/u))
Moti Korets
  • 3,738
  • 2
  • 26
  • 35