How to convert escape characters in HTML tags?
You can use the strconv.Unquote()
to do the conversion.
One thing you should be aware of is that strconv.Unquote()
can only unquote strings that are in quotes (e.g. start and end with a quote char "
or a back quote char `
), so we have to manually append that.
Example:
// Important to use backtick ` (raw string literal)
// else the compiler will unquote it (interpreted string literal)!
s := `\u003chtml\u003e`
fmt.Println(s)
s2, err := strconv.Unquote(`"` + s + `"`)
if err != nil {
panic(err)
}
fmt.Println(s2)
Output (try it on the Go Playground):
\u003chtml\u003e
<html>
Note: To do HTML text escaping and unescaping, you can use the html
package. Quoting its doc:
Package html provides functions for escaping and unescaping HTML text.
But the html
package (specifically html.UnescapeString()
) does not decode unicode sequences of the form \uxxxx
, only decimal;
or HH;
.
Example:
fmt.Println(html.UnescapeString(`\u003chtml\u003e`)) // wrong
fmt.Println(html.UnescapeString(`<html>`)) // good
fmt.Println(html.UnescapeString(`<html>`)) // good
Output (try it on the Go Playground):
\u003chtml\u003e
<html>
<html>
Note #2:
You should also note that if you write a code like this:
s := "\u003chtml\u003e"
This quoted string will be unquoted by the compiler itself as it is an interpreted string literal, so you can't really test that. To specify quoted string in the source, you may use the backtick to specify a raw string literal or you may use a double quoted interpreted string literal:
s := "\u003chtml\u003e" // Interpreted string literal (unquoted by the compiler!)
fmt.Println(s)
s2 := `\u003chtml\u003e` // Raw string literal (no unquoting will take place)
fmt.Println(s2)
s3 := "\\u003chtml\\u003e" // Double quoted interpreted string literal
// (unquoted by the compiler to be "single" quoted)
fmt.Println(s3)
Output:
<html>
\u003chtml\u003e
Fastest method to escape HTML tags as HTML entities?
You could try passing a callback function to perform the replacement:
var tagsToReplace = {
'&': '&',
'<': '<',
'>': '>'
};
function replaceTag(tag) {
return tagsToReplace[tag] || tag;
}
function safe_tags_replace(str) {
return str.replace(/[&<>]/g, replaceTag);
}
Here is a performance test: http://jsperf.com/encode-html-entities to compare with calling the replace
function repeatedly, and using the DOM method proposed by Dmitrij.
Your way seems to be faster...
Why do you need it, though?
Can I escape HTML special chars in JavaScript?
Here's a solution that will work in practically every web browser:
function escapeHtml(unsafe)
{
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
If you only support modern web browsers (2020+), then you can use the new replaceAll function:
const escapeHtml = (unsafe) => {
return unsafe.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", ''');
}
Converting escape sequences to HTML
I have found the issue and how to fix it! (Thank you for all your answers)
I didn't realize I was reading the string from an input and therefore the escape sequences were being interpreted as literal!
I managed to fix this by using JSON.parse()
, turning the literals to escape sequences and now it is accurately represented using white-space: pre-wrap;
the the output's CSS!
Escape characters in string exclude html tags
See:
https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringEscapeUtils.html#escapeHtml4%28java.lang.String%29
public static void main(String[] args) {
String s = "a b c d ö";
System.out.println(StringEscapeUtils.escapeHtml4(s));
}
How do I escape html tags in javascript?
Look at what you have.
- The character
<
- Which needs to be expressed (because it is assigned to
innerHTML
) as HTML so the<
has to be written<
- But that is embedded inside an HTML attribute, where it will be interpreted as HTML when the HTML parser constructs the attribute value in the DOM.
&
has special meaning in HTML, so the&
must be represented as&
So <
Convert special characters to HTML in JavaScript
You need a function that does something like
return mystring.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
But taking into account your desire for different handling of single/double quotes.
Related Topics
Draw Single Pixel Line in HTML5 Canvas
W3C Markup Validator Ampersand (&) Error
Preserve Dynamically Changed HTML on Back Button
Overlaying a Div on Top of HTML 5 Video
Svg Line Markers Not Updating When Line Moves in Ie10
Text Not Wrapping in Paragraph Element
How to Include Glyphicons in Bootstrap 3
What Is The Order of Loading The CSS Files in a HTML Page
Fixed Width Div on Right, Fill Remaining Width Div on Left
Why Is My .Sticky-Top Class Not Working in Bootstrap 4
Object-Fit Not Affecting Images
What HTML/CSS Attributes Are Mail Safe
Horizontal Line and Right Way to Code It in HTML, CSS
Textarea to Fill a Parent Container Exactly, with Padding
Responsive Video Iframes (Keeping Aspect Ratio) with Only CSS
How to Put Text Over an Image Without Absolute Positioning or Setting The Image as Backbround