Skip to content

Instantly share code, notes, and snippets.

@mattfarina
Forked from technosophos/gist:659948
Created November 2, 2010 17:19
Show Gist options
  • Save mattfarina/659957 to your computer and use it in GitHub Desktop.
Save mattfarina/659957 to your computer and use it in GitHub Desktop.
function checkPlain(str) {
var length,
r = new RegExp('(&)|(\")|(<)|(>)|([^&\"<>]*)', 'g')
replacements = ["&amp;", "&quot;", "&lt;", "&gt;"],
retval = '',
match = r.exec(str);
while (match[0] != "") {
length = match.length;
for (var i = 1; i < length; ++i) {
if (match[i] != undefined) {
retval += (i == 5 ? match[i] : replacements[i-1]);
}
}
match = r.exec(str);
}
return retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment