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

javascript detect input character is traditional Chinese or simplified Chinese - Stack Overflow

programmeradmin8浏览0评论

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

1 Answer 1

Reset to default 7

I'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 */
发布评论

评论列表(0)

  1. 暂无评论