JavaScript Get real length of a string (without entities)

前端 未结 5 621
别跟我提以往
别跟我提以往 2021-02-05 10:27

I need to determine the length of string which may contain html-entities.

For example \"&darr ;\" (↓) would return length 6, which is correct, but I want these

5条回答
  •  庸人自扰
    2021-02-05 11:12

    You could for most purposes assume that an ampersand followed by letters, or a possible '#' and numbers, followed by a semicolon, is one character.

    var strlen=string.replace(/&#?[a-zA-Z0-9]+;/g,' ').length;
    

提交回复
热议问题