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

javascript - Regex match multibyte numbers - Stack Overflow

programmeradmin1浏览0评论

I need to match multi-byte 0123456789 characters from Japanese using a regular expression.

[0-9] does not work in this case. How can I got about making this regex? This is my first foray into matching multi-byte strings.

UPDATE

Matching a 4 digit string, such as birth year, was successful with both UTF-8 and non UTF-8 using the following regex

^([0-9]{4}||[\uFF10-\uFF19]{4})$

I need to match multi-byte 0123456789 characters from Japanese using a regular expression.

[0-9] does not work in this case. How can I got about making this regex? This is my first foray into matching multi-byte strings.

UPDATE

Matching a 4 digit string, such as birth year, was successful with both UTF-8 and non UTF-8 using the following regex

^([0-9]{4}||[\uFF10-\uFF19]{4})$

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Aug 8, 2013 at 19:29 user888750user888750 2
  • 0123456789is unicode? – Robert Harvey Commented Aug 8, 2013 at 19:31
  • 2 If it's unicode, see this link – gr3co Commented Aug 8, 2013 at 19:33
Add a ment  | 

2 Answers 2

Reset to default 5

The regex equivalent to /[0-9]/ for these multi-byte numbers in Javascript is

/[\uff10-\uff19]/
var str = '0123456789';
console.log(
    str.match(new RegExp('[0-9]', 'g')),
    str.match(/[\uff10-\uff19]/g) 
);
//returns ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] both ways

Make sure to save the .js file with the proper encoding (UTF-8) if using the unescaped version.

发布评论

评论列表(0)

  1. 暂无评论