Javascript converts double byte characters to single byte characters and calculates length

  • 2021-06-29 10:08:55
  • OfStack

function(s){
    return Math.ceil(s.replace(/[\u4e00-\u9fa5]/g, 'xx').length / 2);
}

You can also use (/[^x00-xff]/g,'**'), which replaces a double-byte character with'**'
(/[u4e00-u9fa5]/g,'**') Is this the unicode edition in Chinese with'**', replacing Chinese characters with'**', u4e00-u9fa5?


Related articles: