Last active
April 5, 2018 14:05
-
-
Save tablatronix/cb9043d0a8075d475d70725069b23789 to your computer and use it in GitHub Desktop.
html excape examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
// https://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery | |
escaped = new Option(unescaped).innerHTML; | |
var entityMap = { | |
'&': '&', | |
'<': '<', | |
'>': '>', | |
'"': '"', | |
"'": ''', | |
'/': '/', | |
'`': '`', | |
'=': '=' | |
}; | |
function escapeHtml (string) { | |
return String(string).replace(/[&<>"'`=\/]/g, function (s) { | |
return entityMap[s]; | |
}); | |
} | |
function escapeHtmlEntities (str) { | |
if (typeof jQuery !== 'undefined') { | |
// Create an empty div to use as a container, | |
// then put the raw text in and get the HTML | |
// equivalent out. | |
return jQuery('<div/>').text(str).html(); | |
} | |
// No jQuery, so use string replace. | |
return str | |
.replace(/&/g, '&') | |
.replace(/>/g, '>') | |
.replace(/</g, '<') | |
.replace(/"/g, '"'); | |
} | |
var escapedHtml = html.replace(/&/g, '&') | |
.replace(/>/g, '>') | |
.replace(/</g, '<') | |
.replace(/"/g, '"') | |
.replace(/'/g, '''); | |
function encode(e){return e.replace(/[^]/g,function(e){return"&#"+e.charCodeAt(0)+";"})} | |
function escapeHtml(text) { | |
'use strict'; | |
return text.replace(/[\"&<>]/g, function (a) { | |
return { '"': '"', '&': '&', '<': '<', '>': '>' }[a]; | |
}); | |
} | |
function escapeHtml(text) { | |
'use strict'; | |
return text.replace(/[\"&'\/<>]/g, function (a) { | |
return { | |
'"': '"', '&': '&', "'": ''', | |
'/': '/', '<': '<', '>': '>' | |
}[a]; | |
}); | |
} | |
</script> | |
String escapeParameter(String param) { | |
param.replace("+"," "); | |
param.replace("%21","!"); | |
param.replace("%23","#"); | |
param.replace("%24","$"); | |
param.replace("%26","&"); | |
param.replace("%27","'"); | |
param.replace("%28","("); | |
param.replace("%29",")"); | |
param.replace("%2A","*"); | |
param.replace("%2B","+"); | |
param.replace("%2C",","); | |
param.replace("%2F","/"); | |
param.replace("%3A",":"); | |
param.replace("%3B",";"); | |
param.replace("%3D","="); | |
param.replace("%3F","?"); | |
param.replace("%40","@"); | |
param.replace("%5B","["); | |
param.replace("%5D","]"); | |
return param; | |
} | |
String urlencode(String str) | |
{ | |
String encodedString=""; | |
char c; | |
char code0; | |
char code1; | |
char code2; | |
for (int i =0; i < str.length(); i++){ | |
c=str.charAt(i); | |
if (c == ' '){ | |
encodedString+= '+'; | |
} else if (isalnum(c)){ | |
encodedString+=c; | |
} else{ | |
code1=(c & 0xf)+'0'; | |
if ((c & 0xf) >9){ | |
code1=(c & 0xf) - 10 + 'A'; | |
} | |
c=(c>>4)&0xf; | |
code0=c+'0'; | |
if (c > 9){ | |
code0=c - 10 + 'A'; | |
} | |
code2='\0'; | |
encodedString+='%'; | |
encodedString+=code0; | |
encodedString+=code1; | |
//encodedString+=code2; | |
} | |
yield(); | |
} | |
return encodedString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment