JavaScript:使用 decodeUri 或 unescape 不会删除空格

JavaScript: Using decodeUri or unescape isn't removing whitespace

本文关键字:删除 空格 unescape 使用 decodeUri JavaScript      更新时间:2023-09-26

我目前正在加载一个WSYWIG编辑器并直接从数据库中读取HTML代码。

var html = "%3Cp%3E%3Ca%20href=%22google%20 : %20//in-app.purchase%22%3E%3Cimg%20src=%22https%20 : %20//uploads/28/content_drawadragon.png%22%20style=%22height%20 : %20100%25;%20width%20 : %20100%25%22%20/%3E%3C/a%3E%3C/p%3E%0D%0A"

但是,使用decodeURI(html)会导致以下结果:

"<p><a href="google :  //in-app.purchase"><img src="https  :  //uploads/28/content_drawadragon.png" style="height  :  100%; width  :  100%" /></a></p>"

我真正想要的是这个:

decodeURI(html) = "<p><a href="google://in-app.purchase"><img src="https: //uploads/28/content_drawadragon.png" style="height:100%;width:100%"/></a></p>"

很乐意帮助尝试用尽可能少的代码解决这个问题,而无需一些疯狂的正则表达式。

去掉解码 URI 之前的空格。

"坏"模式似乎是一个转义空间,后跟空格、冒号、空格和转义空间,因此请使用此正则表达式:

decodeURI(html.replace(/%20 : %20/g, ":"))