Answer the question
In order to leave comments, you need to log in
How to decode a string that comes from the server?
Hello, tell me please.
On the server, as well as on the client, there is a check that if the user enters html tags, then they need to be encoded, the function looks like this:
var encodeHtmlEntities = function (str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\\/g, '\')
.replace(/\//g, '/');
};
<script>alert(1)</script>
<script>alert(1)</script>
Answer the question
In order to leave comments, you need to log in
function encodeHtmlEntities(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\\/g, '\')
.replace(/\//g, '/');
};
function decodeHtmlEntities(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, '\'')
.replace(/\/g, '\\')
.replace(///g, '/');
};
var str = '<script src="test.com">\\Lorem Ipsu\'m</script>';
str = encodeHtmlEntities(str);
console.log(str); // <script src="test.com">\Lorem Ipsu'm</script>
str = decodeHtmlEntities(str);
console.log(str); // <script src="test.com">\Lorem Ipsu'm</script>
The decode function should look like the encode function. Just the opposite.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question