JavaScript Get real length of a string (without entities)

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

    If you are running the javascript in a browser I would suggest using it to help you. You can create an element and set its innerHTML to be your string containing HTML-entities. Then extract the contents of that element you just created as text.

    Here is an example (uses Mootools): http://jsfiddle.net/mqchen/H73EV/

提交回复
热议问题