最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Japanese Regex for Numbers - Stack Overflow

programmeradmin0浏览0评论

** 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
Add a ment  | 

2 Answers 2

Reset to default 9

Your 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

发布评论

评论列表(0)

  1. 暂无评论