amp 显示成转义字符 in html,如何在HTML标签中转换转义字符?(How to convert escape characters in HTML tags?)...
如何在HTML標簽中轉換轉義字符?(How to convert escape characters in HTML tags?)
我們如何直接將"\u003chtml\u003e"轉換為"" ? 使用json.Marshal()將""轉換為"\u003chtml\u003e"非常簡單,但json.Unmarshal()非常冗長且繁瑣。 在golang有沒有直接的方法呢?
How can we directly convert "\u003chtml\u003e" to ""? Conversion of "" to "\u003chtml\u003e" is quite easy using json.Marshal(), but json.Unmarshal() is quite lengthy and cumbersome. Is there any direct way to do that in golang?
原文:https://stackoverflow.com/questions/36528575
更新時間:2019-09-26 09:46
最滿意答案
您應該注意的一件事是strconv.Unquote()只能strconv.Unquote()引號中的引號(例如,以引號char或后引號char `開頭和結尾),因此我們必須手動追加它。
例:
// 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)
\u003chtml\u003e
注意:要執行HTML文本轉義和非轉義,您可以使用html包。 引用其文檔:
包html提供了轉義和轉義HTML文本的功能。
但是html包(特別是html.UnescapeString() )不能解碼形式為\uxxxx unicode序列,只有decimal; 或HH; 。
例:
fmt.Println(html.UnescapeString(`\u003chtml\u003e`)) // wrong
fmt.Println(html.UnescapeString(`<html>`)) // good
fmt.Println(html.UnescapeString(`<html>`)) // good
\u003chtml\u003e
筆記2:
您還應該注意,如果您編寫如下代碼:
s := "\u003chtml\u003e"
這個引用的字符串將被編譯器本身取消引用,因為它是一個解釋的字符串文字 ,所以你無法真正測試它。 要在源中指定帶引號的字符串,可以使用反引號指定原始字符串文字,或者可以使用雙引號解釋的字符串文字:
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)
輸出:
\u003chtml\u003e
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
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
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:
\u003chtml\u003e
相關問答
簡短的回答:
還有另一種選擇:
${fn:escapeXml(myString)}
Short answer:
there is another option:
...
StringUtils.replaceEach(str, new String[]{"&", "\"", ""}, new String[]{"&", """, "<", ">"})
StringUtils.replaceEach(str, new String[]{"&", "\"", ""}, new String[]{"&", """, "<", ">"})
你甚至不需要在這個字符串中轉義雙引號 - 只有在用雙引號括起你的字符串時才需要。 你不應該逃避你的/ es; 它們不會破壞字符串而document.write() (afaik)允許插入純HTML。 You don't even need to escape the double quotes in this string - that's only necessary if you enclose your string with double quotes. You shouldn't nee
...
您可以嘗試傳遞回調函數來執行替換: var tagsToReplace = {
'&': '&',
'
'>': '>'
};
function replaceTag(tag) {
return tagsToReplace[tag] || tag;
}
function safe_tags_replace(str) {
return str.replace(/[&<>]/g, replaceTag);
}
以下是一個
...
您可以使用strconv.Unquote()進行轉換。 您應該注意的一件事是strconv.Unquote()只能strconv.Unquote()引號中的引號(例如,以引號char或后引號char `開頭和結尾),因此我們必須手動追加它。 例: // Important to use backtick ` (raw string literal)
// else the compiler will unquote it (interpreted string literal)!
s := `\
...
現在我找到了以下解決方案:我已將CharacterEscapes添加到ObjectMapper類的JsonFactory中。 我也改變了將JSON寫入響應的方式。 代替 objectMapper.writeValue(response.getWriter(), myObject)
我這樣做: PrintWriter writer = response.getWriter();
writer.print(String.valueOf(objectMapper.writeValueAsBytes(m
...
在我的termsFactory里面(我在JSON GET調用中處理和保存數據對象) 我循環遍歷它們并使用$sce創建了trustAsHTML字符串 TermsController: // Set terms into data model:
TermsFactory.setTermsModel(data.data.ticker_tags);
// Build tag widgets with ng-repeat:
vm.terms = TermsFactory.getTermsMode
...
if (character == '
result.append("<");
}
else if (character == '>') {
result.append(">");
// ...
刪除它。 你不需要它。 JSTL 已經完成了這項工作。
否則,您的HTML字符串將被轉義兩次。 每個&成為& 再一次等等。 如果你真的需要自己動手逃跑(為什么
...
我使用SO使用的東西。 它是opensource并具有多種語言的解析器。 名字是WMD ,問題是“WMD編輯器開源項目在哪里?” 有一些QA材料概述了這個編輯器。 “運行showdown.js服務器以將Markdown轉換為HTML(在PHP中)”的問題有一些QA材料概述了PHP中的一些Markdown庫。 I use what SO uses. it is opensource and has parsers for many languages. The name is WMD and the
...
我不知道框架中包含任何內容,但你可以試試這樣的東西嗎? public string ConvertToHtmlEntities(string plainText)
{
StringBuilder sb = new StringBuilder(plainText.Length * 6);
foreach (char c in plainText)
{
sb.Append("").Append((ushort)c).Append(';');
}
...
總結
以上是生活随笔為你收集整理的amp 显示成转义字符 in html,如何在HTML标签中转换转义字符?(How to convert escape characters in HTML tags?)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王者荣耀五岳打卡地点在哪 王者荣耀五岳打
- 下一篇: html个版本间的特点,了解下什么是HT