function escapeHtml(text) {
return text
.replace(/\t/g, "")
.replace(/\n/g, "")
.replace(/%/g,"")
.replace(/\s/g, " ")
.replace(/&/g, "")
.replace(/</g, "")
.replace(/>/g, "")
}
can somebody provide me the regex for all the above entities into a single regex?
2 Answers 2
If \s is being replaced with a space, while all the others are being replaced with the empty string, it's not possible with a single regex unless you provide it with a replacer function, which doesn't make much sense - just use two .replaces. To be concise, use a character set:
return text
.replace(/[\t\n%&<>]/g, '')
.replace(/\s/g, ' ');
answered Sep 4, 2018 at 3:52
CertainPerformance
374k55 gold badges354 silver badges359 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Try this:
text.replace(/[&%$<>\t\s]/g,"");
replace all enter, whitespaces & tabs with \s and \t
and character &%$<>
Comments
lang-js
'<div>something</div>' -> 'divsomething/div'\swith a space, instead of with an empty string like all the rest?\sis the same as space,\t, and\n. but you already replaced\tand\nearlier.