Centralize text encoding
This commit is contained in:
parent
20f4849fee
commit
3eae453de3
9 changed files with 23 additions and 34 deletions
|
@ -38,25 +38,7 @@ export function clean(text) {
|
|||
return text;
|
||||
}
|
||||
|
||||
// regexEscape(string) to escape characters in a string for use in a regex
|
||||
export function regexEscape(string) {
|
||||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
||||
}
|
||||
|
||||
// decodeEntities(string)
|
||||
export function decodeEntities(string) {
|
||||
var translate_re = /&(nbsp|amp|quot|lt|gt);/g;
|
||||
var translate = {
|
||||
"nbsp": " ",
|
||||
"amp": "&",
|
||||
"quot": "\"",
|
||||
"lt": "<",
|
||||
"gt": ">"
|
||||
};
|
||||
return string.replace(translate_re, function(match, entity) {
|
||||
return translate[entity];
|
||||
}).replace(/&#(\d+);/gi, function(match, numStr) {
|
||||
var num = parseInt(numStr, 10);
|
||||
return String.fromCharCode(num);
|
||||
});
|
||||
}
|
||||
// textEncode(string) to encode characters for image processing
|
||||
export function textEncode(string) {
|
||||
return string.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n").replaceAll("\\:", ":");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue