关于中英文标点符号的unicode范围

326 阅读1分钟
类型unicode范围说明
常用中文\u4E00 - \u9FFF
中文扩展\u3400 - \u4DBF、\u20000+比如生僻字等
英文\u0041 - \u005A、\u0061 - \u007A大小写英文a-z,A-Z
数字\u0030 - \u00390-9
中文标点\u3000 - \u303F,\uFF00 - \uFFEF如,。!等
英文标点\u0020 - \u007E如!"#$%& 等

用例

//比如有
const text="测试中文英文english1234,.,。"

function getCharType(char) {
  const code = char.codePointAt(0);

  if ((code >= 0x4E00 && code <= 0x9FFF)|| code >= 0x20000) return 'chinese';
  if ((code >= 0x0041 && code <= 0x005A) || (code >= 0x0061 && code <= 0x007A)) return 'english';
  if (code >= 0x0030 && code <= 0x0039) return 'number';
  if ((code >= 0x3000 && code <= 0x303F) || (code >= 0xFF00 && code <= 0xFFEF)) return 'chinesePunctuation';
  if (code >= 0x0020 && code <= 0x007E) return 'englishPunctuation';
  return 'other';
}

// 示例用法
const result = [...text].map(c => ({ char: c, type: getCharType(c) }));
console.log(result);