Created
January 21, 2022 14:05
-
-
Save taimursaeed/549218bc66505f1421f29e8a6be7d6aa to your computer and use it in GitHub Desktop.
Escape/Unescape quotes from a string
This file contains hidden or 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 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