JavaScript Get real length of a string (without entities)

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

    Unfortunately, JavaScript does not natively support encoding or decoding of HTML entities, which is what you will need to do to get the 'real' string length. I was able to find this third-party library which is able to decode and encode HTML entities and it appears to work well enough, but there's no guaranteeing how complete it will be.

    http://www.strictly-software.com/htmlencode

提交回复
热议问题