日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

amp 显示成转义字符 in html,如何在HTML标签中转换转义字符?(How to convert escape characters in HTML tags?)...

發(fā)布時(shí)間:2023/12/19 HTML 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 amp 显示成转义字符 in html,如何在HTML标签中转换转义字符?(How to convert escape characters in HTML tags?)... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

如何在HTML標(biāo)簽中轉(zhuǎn)換轉(zhuǎn)義字符?(How to convert escape characters in HTML tags?)

我們?nèi)绾沃苯訉?#34;\u003chtml\u003e"轉(zhuǎn)換為"" ? 使用json.Marshal()將""轉(zhuǎn)換為"\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

更新時(shí)間:2019-09-26 09:46

最滿意答案

您應(yīng)該注意的一件事是strconv.Unquote()只能strconv.Unquote()引號(hào)中的引號(hào)(例如,以引號(hào)char或后引號(hào)char `開頭和結(jié)尾),因此我們必須手動(dòng)追加它。

例:

// 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

注意:要執(zhí)行HTML文本轉(zhuǎn)義和非轉(zhuǎn)義,您可以使用html包。 引用其文檔:

包html提供了轉(zhuǎn)義和轉(zhuǎn)義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:

您還應(yīng)該注意,如果您編寫如下代碼:

s := "\u003chtml\u003e"

這個(gè)引用的字符串將被編譯器本身取消引用,因?yàn)樗且粋€(gè)解釋的字符串文字 ,所以你無法真正測(cè)試它。 要在源中指定帶引號(hào)的字符串,可以使用反引號(hào)指定原始字符串文字,或者可以使用雙引號(hào)解釋的字符串文字:

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

相關(guān)問答

簡短的回答:

還有另一種選擇:

${fn:escapeXml(myString)}

Short answer:

there is another option:

...

StringUtils.replaceEach(str, new String[]{"&", "\"", ""}, new String[]{"&", """, "<", ">"})

StringUtils.replaceEach(str, new String[]{"&", "\"", ""}, new String[]{"&", """, "<", ">"})

你甚至不需要在這個(gè)字符串中轉(zhuǎn)義雙引號(hào) - 只有在用雙引號(hào)括起你的字符串時(shí)才需要。 你不應(yīng)該逃避你的/ es; 它們不會(huì)破壞字符串而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

...

您可以嘗試傳遞回調(diào)函數(shù)來執(zhí)行替換: var tagsToReplace = {

'&': '&',

'

'>': '>'

};

function replaceTag(tag) {

return tagsToReplace[tag] || tag;

}

function safe_tags_replace(str) {

return str.replace(/[&<>]/g, replaceTag);

}

以下是一個(gè)

...

您可以使用strconv.Unquote()進(jìn)行轉(zhuǎn)換。 您應(yīng)該注意的一件事是strconv.Unquote()只能strconv.Unquote()引號(hào)中的引號(hào)(例如,以引號(hào)char或后引號(hào)char `開頭和結(jié)尾),因此我們必須手動(dòng)追加它。 例: // Important to use backtick ` (raw string literal)

// else the compiler will unquote it (interpreted string literal)!

s := `\

...

現(xiàn)在我找到了以下解決方案:我已將CharacterEscapes添加到ObjectMapper類的JsonFactory中。 我也改變了將JSON寫入響應(yīng)的方式。 代替 objectMapper.writeValue(response.getWriter(), myObject)

我這樣做: PrintWriter writer = response.getWriter();

writer.print(String.valueOf(objectMapper.writeValueAsBytes(m

...

在我的termsFactory里面(我在JSON GET調(diào)用中處理和保存數(shù)據(jù)對(duì)象) 我循環(huán)遍歷它們并使用$sce創(chuàng)建了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 已經(jīng)完成了這項(xiàng)工作。

否則,您的HTML字符串將被轉(zhuǎn)義兩次。 每個(gè)&成為& 再一次等等。 如果你真的需要自己動(dòng)手逃跑(為什么

...

我使用SO使用的東西。 它是opensource并具有多種語言的解析器。 名字是WMD ,問題是“WMD編輯器開源項(xiàng)目在哪里?” 有一些QA材料概述了這個(gè)編輯器。 “運(yùn)行showdown.js服務(wù)器以將Markdown轉(zhuǎn)換為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

...

我不知道框架中包含任何內(nèi)容,但你可以試試這樣的東西嗎? public string ConvertToHtmlEntities(string plainText)

{

StringBuilder sb = new StringBuilder(plainText.Length * 6);

foreach (char c in plainText)

{

sb.Append("").Append((ushort)c).Append(';');

}

...

總結(jié)

以上是生活随笔為你收集整理的amp 显示成转义字符 in html,如何在HTML标签中转换转义字符?(How to convert escape characters in HTML tags?)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。