JavaScript Get real length of a string (without entities)

前端 未结 5 559
别跟我提以往
别跟我提以往 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:13

    Since there's no solution using jQuery yet:

    var str = 'lol&';
    alert($('').html(str).text().length); // alerts 4
    

    Uses the same approach like karim79, but it never adds the created element to the document.

提交回复
热议问题