Determine if string is encoded HTML via jQuery?

 ̄綄美尐妖づ 提交于 2019-12-23 02:52:22

问题


I'm working on a validation script, but I'm running into a very particular issue.

If a user enters a string that happens to be an encoded html character (like & or &), it will output as the character (& in this case). My question is this: is it possible to write a function that detemines if a string is an encoded character? So if the user enters one of the two above options, I want to launch a particular function, and if it's a non-encoded character, I want to do something else.

Is there a way to do this?


回答1:


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).




回答2:


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.




回答3:


Something like this would do it.

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

    return rHTMLEncoded.test(val) ;
}


// Usage 
var encoded = containsEncoded("&amp;");


来源:https://stackoverflow.com/questions/13703312/determine-if-string-is-encoded-html-via-jquery

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