admin 管理员组文章数量: 1086019
** Question:** How do I allow only numbers in japanese(Hiragana/Katagana)
Use case: I want to take "number" field (My input should only accept numbers from English and Japanese)
I've written the following sample snippet:
// The goal of this demo is to demonstrate the RegEx patterns for English and Japanese Chracters
var english = "09c12";
var japanese = "0123あb";
console.log("-----English Test---")
console.log(english.replace(/[^0-9\/]/gi, ''));
console.log("--------------------")
console.log("-----japanese Test---")
console.log(japanese.replace(/[^0-9\/]/gi, ''));
console.log("--------------------")
** Question:** How do I allow only numbers in japanese(Hiragana/Katagana)
Use case: I want to take "number" field (My input should only accept numbers from English and Japanese)
I've written the following sample snippet:
// The goal of this demo is to demonstrate the RegEx patterns for English and Japanese Chracters
var english = "09c12";
var japanese = "0123あb";
console.log("-----English Test---")
console.log(english.replace(/[^0-9\/]/gi, ''));
console.log("--------------------")
console.log("-----japanese Test---")
console.log(japanese.replace(/[^0-9\/]/gi, ''));
console.log("--------------------")
Issue: This is not working for Japanese. Please enlighten me.
I am assuming Japanese characters have different ASCII/Unicode values?
Please help me fix the code. I want to just make sure the user enters the numbers.
Thanks
Share Improve this question edited Sep 19, 2017 at 2:08 TechnoCorner asked Sep 19, 2017 at 1:51 TechnoCornerTechnoCorner 5,17510 gold badges45 silver badges89 bronze badges 2-
0123あsd2
... what are these numbers exactly? I think in kanji the numbers for Chinese, Japanese, and Korean would all look about the same, and not this, which appears to be a mixture of Arabic and characters. In any case, you're probably going to have to use unicode literals here. Update your question and someone can hopefully answer. – Tim Biegeleisen Commented Sep 19, 2017 at 1:58 -
It's
0123ab
in hiragana. Updated this in question @TimBiegeleisen – TechnoCorner Commented Sep 19, 2017 at 2:00
2 Answers
Reset to default 9Your Japanese digits are not simple ASCII digits, they are unicode fullwidth characters, (see http://www.fileformat.info/info/unicode/char/ff10/index.htm)
the regex syntax in javascript doesn't have unicode classes, so you'll have to select for them by manually specifying the unicode range.
console.log("0123あb".replace(/[\uff10-\uff19]/g, "_"));
console.log("0123あb".replace(/[^\uff10-\uff19]/g, "_"));
console.log(String.fromCharCode(...[...Array(10)].map((x, i) => 0xff10 + i))
);
Is this useful for you?
var english = "09c12";
var japanese = "0123あsd2";
console.log("-----English Test---")
console.log(english.replace(/[^0-9\/]/gi, ''));
console.log("--------------------")
console.log("-----japanese Test---")
console.log(japanese.replace(/[^0-9]/g, ''));
console.log("--------------------")
JSFiddle
本文标签: javascriptJapanese Regex for NumbersStack Overflow
版权声明:本文标题:javascript - Japanese Regex for Numbers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744051778a2525149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论