javascript 日语字符的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6787716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:52:48  来源:igfitidea点击:

Regular Expression for Japanese characters

javascriptregexunicodeinternationalizationcjk

提问by Nilesh Shukla

I am doing internationalization in Struts. I want to write Javascript validation for Japanese and English users. I know regular expression for English but not for Japanese users. Is it possible to write one regular expression for both the users which validate on the basis of Unicode?

我正在 Struts 中进行国际化。我想为日语和英语用户编写 Javascript 验证。我知道英语的正则表达式,但不知道日语用户的正则表达式。是否可以为基于 Unicode 验证的两个用户编写一个正则表达式?

Please help me.

请帮我。

回答by shawndreck

Here is a regular expression that can be used to match all English alphanumeric characters, Japanese katakana, hiragana, multibytes of alphanumerics (hankaku and zenkaku), and dashes:

这是一个正则表达式,可用于匹配所有英文字母数字字符、日语片假名、平假名、多字节字母数字(半角和前角)和破折号:

/[一-龠]+|[ぁ-?]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+|[々〆〤]+/u

You can edit it to fit your needs, but notice the "u" flag at the end.

您可以编辑它以满足您的需要,但请注意末尾的“u”标志。

I hope this helps!

我希望这有帮助!

回答by spraff

Provided your text editor and programming language support Unicode, you should be able to enter Japanese characters as literal strings. Things like [A-X]ranges will probably not translate very well in general.

如果您的文本编辑器和编程语言支持 Unicode,您应该能够将日语字符作为文字字符串输入。[A-X]一般来说,范围之类的东西可能不会很好地翻译。

What kind of text are you trying to validate?

你想验证什么样的文本?

What language are the regular experssions in? Perl-compatible, POSIX, or something else?

正则表达式是什么语言?Perl 兼容、POSIX 或其他什么?

回答by Andrew

As long as you save your scripts in the same character set as your page (e.g. both HTML and JavaScript are UTF-8 or both HTML and JavaScript are Shift_JIS), you should be able to treat your regular expressions exactly the same as you would with English.

只要您以与页面相同的字符集保存脚本(例如,HTML 和 JavaScript 都是 UTF-8,或者 HTML 和 JavaScript 都是 Shift_JIS),您应该能够像对待正则表达式一样对待正则表达式英语。

function isKansai(city) {
    var rxKansai = /(大阪|兵庫|京都|滋賀|奈良|和歌山|osaka|hyo{1,2}go|kyoto|shiga|nara|wakayama)/i;
    return rxKansai.test(city);
}
isKansai('東京'); // false
isKansai('大阪'); // true
isKansai('Tokyo'); // false
isKansai('Osaka') // true