Determine if string is encoded HTML via jQuery?

你。 提交于 2019-12-08 18:17:30

By definition, if you do not know whether something is an encoded HTML entity or not you do not know. Either you treat all text coming from a certain source as encoded or not encoded. Why? Because it's all just text. "&" is just text. I meant to write "&" here. I do not want anyone to interpret it, I want it to appear literally as "&".

How do you know what the user meant? If you're starting to replace user-entered text based on guesses, you'll always screw it up in some cases. It's the typical case where all ":D" is replaced by a graphical smilie, which is annoying when you actually wanted to type ":D".

If you want to always preserve exactly what the user entered, always run all user input through an HTML-encoding function which replaces all special characters with entities. See The Great Escapism (Or: What You Need To Know To Work With Text Within Text).

You can check if a string contains encoded characters by comparing the encoded vs decoded lengths:

var string = "Your encoded & decoded string here"

function decode(str){
    return decodeURIComponent(str).replace(/&lt;/g,'<').replace(/&gt;/g,'>');
}

if(string.length == decode(string).length){
    // The string does not contain any encoded html.
}else{
    // The string contains encoded html.
}

Also, this is significantly faster than the jQuery method that was suggested.

Something like this would do it.

function containsEncoded (val){
    var rHTMLEncoded = /&[^\s]*/;

    return rHTMLEncoded.test(val) ;
}


// Usage 
var encoded = containsEncoded("&amp;");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!