Skip to content

Instantly share code, notes, and snippets.

@aliakbarRashidi
Forked from atesgoral/LICENSE.txt
Created November 21, 2021 06:11
Show Gist options
  • Select an option

  • Save aliakbarRashidi/fdd783eaed6708acd55f890a01aa9477 to your computer and use it in GitHub Desktop.

Select an option

Save aliakbarRashidi/fdd783eaed6708acd55f890a01aa9477 to your computer and use it in GitHub Desktop.
XML-escape given string
function (s) {
return s.replace(
/[&<>'"]/g, // All characters we want to escape
function (c){
return "&" // prefix
+ { // lookup map
"&": "amp",
"<": "lt",
">": "gt",
"'": "apos",
'"': "quot"
}[c]
+ ";" // suffix
})
}
function(s){return s.replace(/[&<>'"]/g,function(c){return "&"+{"&":"amp","<":"lt",">":"gt","'":"apos",'"':"quot"}[c]+";"})}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Ates Goral <http://magnetiq.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "xmlEscape",
"description": "Escape basic XML characters",
"keywords": [
"string",
"escaping",
"xml"
]
}
var xmlEscape = function(s){return s.replace(/[&<>'"]/g,function(c){return "&"+{"&":"amp","<":"lt",">":"gt","'":"apos",'"':"quot"}[c]+";"})};
console.log(xmlEscape("testing <element attribute=\"value\"> & apostrophe (')"));
// testing &lt;element attribute=&quot;value&quot;&gt; &amp; apostrophe (&apos;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment