Skip to content

Instantly share code, notes, and snippets.

@technosophos
Created November 2, 2010 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save technosophos/659948 to your computer and use it in GitHub Desktop.
Save technosophos/659948 to your computer and use it in GitHub Desktop.
Faster checkPlain()?
function checkPlain(str) {
var r = new RegExp('(&)|(\")|(<)|(>)|([^&\"<>]*)', 'g');
var replacements = ["&amp;", "&quot;", "&lt;", "&gt;"];
var retval = '';
var match = r.exec(str);
while (match[0] != "") {
for (var i = 1; i < match.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