Skip to content

Instantly share code, notes, and snippets.

@taimursaeed
Created January 21, 2022 14:05
Show Gist options
  • Save taimursaeed/549218bc66505f1421f29e8a6be7d6aa to your computer and use it in GitHub Desktop.
Save taimursaeed/549218bc66505f1421f29e8a6be7d6aa to your computer and use it in GitHub Desktop.
Escape/Unescape quotes from a string
var utility = {
escapeDoubleQuotes: function (string) {
return string.replace(/"/g, """);
},
unescapeDoubleQuotes: function (string) {
return string.replace(/"/g, '"');
},
escapeSingleQuotes: function (string) {
return string.replace(/'/g, "'");
},
unescapeSingleQuotes: function (string) {
return string.replace(/'/g, "'");
},
escapeQuotes: function (str) {
return String(str).replace(/"/g, """).replace(/'/g, "'");
},
unescapeQuotes: function (str) {
return String(str)
.replace(/"/g, '"')
.replace(/'/g, "'");
},
};
var strSingle = "Saved Response English Title '1'";
console.log(utility.escapeSingleQuotes(strSingle));
console.log(
utility.unescapeSingleQuotes(utility.escapeSingleQuotes(strSingle))
);
var strDouble = 'Saved Response English Title "1"';
console.log(utility.escapeDoubleQuotes(strDouble));
console.log(
utility.unescapeDoubleQuotes(utility.escapeDoubleQuotes(strDouble))
);
var strSingleDouble = `Saved Response 'English' Title "1"`; //?
console.log(utility.escapeQuotes(strSingleDouble));
console.log(utility.unescapeQuotes(utility.escapeQuotes(strSingleDouble)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment