-
-
Save atesgoral/989212 to your computer and use it in GitHub Desktop.
XML-escape given string
This file contains 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
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 | |
}) | |
} |
This file contains 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
function(s){return s.replace(/[&<>'"]/g,function(c){return "&"+{"&":"amp","<":"lt",">":"gt","'":"apos",'"':"quot"}[c]+";"})} |
This file contains 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
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. |
This file contains 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
{ | |
"name": "xmlEscape", | |
"description": "Escape basic XML characters", | |
"keywords": [ | |
"string", | |
"escaping", | |
"xml" | |
] | |
} |
This file contains 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
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 <element attribute="value"> & apostrophe (') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I win, 39 byte