27 lines
861 B
JavaScript
27 lines
861 B
JavaScript
|
|
//#region src/string/unescape.ts
|
||
|
|
const htmlUnescapes = {
|
||
|
|
"&": "&",
|
||
|
|
"<": "<",
|
||
|
|
">": ">",
|
||
|
|
""": "\"",
|
||
|
|
"'": "'"
|
||
|
|
};
|
||
|
|
/**
|
||
|
|
* Converts the HTML entities `&`, `<`, `>`, `"`, and `'` in `str` to their corresponding characters.
|
||
|
|
* It is the inverse of `escape`.
|
||
|
|
*
|
||
|
|
* @param {string} str The string to unescape.
|
||
|
|
* @returns {string} Returns the unescaped string.
|
||
|
|
*
|
||
|
|
* @example
|
||
|
|
* unescape('This is a <div> element.'); // returns 'This is a <div> element.'
|
||
|
|
* unescape('This is a "quote"'); // returns 'This is a "quote"'
|
||
|
|
* unescape('This is a 'quote''); // returns 'This is a 'quote''
|
||
|
|
* unescape('This is a & symbol'); // returns 'This is a & symbol'
|
||
|
|
*/
|
||
|
|
function unescape(str) {
|
||
|
|
return str.replace(/&(?:amp|lt|gt|quot|#(0+)?39);/g, (match) => htmlUnescapes[match] || "'");
|
||
|
|
}
|
||
|
|
//#endregion
|
||
|
|
export { unescape };
|