admin管理员组文章数量:1400041
I am new to web develop and now developing an internal use website by js for different languages user, Its worked great for detect English character input and Chinese character input . and I want further more to detect traditional and simplified Chinese . Any idea to done this task ?
below is my code to detect chinese and english
if (string.match(/^[A-Za-z]*$/)) {
//....English
} else if (string.match(/[\u3400-\u9FBF]/)) {
//....Chinese
} else {
}
I am new to web develop and now developing an internal use website by js for different languages user, Its worked great for detect English character input and Chinese character input . and I want further more to detect traditional and simplified Chinese . Any idea to done this task ?
below is my code to detect chinese and english
if (string.match(/^[A-Za-z]*$/)) {
//....English
} else if (string.match(/[\u3400-\u9FBF]/)) {
//....Chinese
} else {
}
Share
Improve this question
asked Sep 1, 2016 at 4:35
Stephen ChenStephen Chen
3,0572 gold badges28 silver badges40 bronze badges
3
- Use Unicode Characters (eg. \u3400) in "string.match" for traditional Chinese detection and another condition to detect simplified Chinese. Please update here if it doesn't work. – user5312477 Commented Sep 1, 2016 at 4:43
- This link may help you. Chinese Unicode Table - StackOverflow – user5312477 Commented Sep 1, 2016 at 4:48
- thx for the resource , I will tried later ... – Stephen Chen Commented Sep 1, 2016 at 6:14
1 Answer
Reset to default 7I've built a library traditional-or-simplified to detect if a string contains a majority of Traditional or Simplified Chinese characters by paring the number of Simplified/Traditional characters that appear in an input string.
var TradOrSimp = require('traditional-or-simplified');
// Detect if a string contains Simplified Chinese
TradOrSimp.isSimplified('无需注册或设置')
// True
// Detect if a string contains Traditional Chinese
TradOrSimp.isTraditional('無需帳戶或註冊。')
// True
// Detect if a string contains Traditional or Simplified Chinese characters
TradOrSimp.detect('無需帳戶或註冊。')
/*
{ inputLength: 8, // Length of input string
simplifiedCharacters: 0, // Count of Simplified Chinese characters
traditionalCharacters: 4, // Count of Traditional Chinese characters
detectedCharacters: 'traditional', // Detected character set
detectionRate: 1 } // Ratio of majority/minority character sets */
本文标签: javascript detect input character is traditional Chinese or simplified ChineseStack Overflow
版权声明:本文标题:javascript detect input character is traditional Chinese or simplified Chinese - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744234207a2596479.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论