700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > amp 显示成转义字符 in html 如何在HTML标签中转换转义字符?(How to convert

amp 显示成转义字符 in html 如何在HTML标签中转换转义字符?(How to convert

时间:2024-08-26 10:24:35

相关推荐

amp 显示成转义字符 in html 如何在HTML标签中转换转义字符?(How to convert

如何在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?

原文:/questions/36528575

更新时间:-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?)...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。