【问题标题】:unicode chars give "unterminated string literal" in jsunicode chars 在 js 中给出“未终止的字符串文字”
【发布时间】:2016-09-01 07:07:10
【问题描述】:

当我的 HTML 中有一些奇怪的字符被视为空格时,会生成此错误。

<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title></title>
</head>
<body>
<p>Some
    Text</p>
</body>
</html>

注意SomeText之间有一个字符,但是这里看不到。我需要将它传递给一个函数 toJson(),但它返回一个错误说 unterminated string literal

当我使用简单的文本而不是这样的时候,一切都很好:

Some&lt;space&gt;Text 工作正常。

我已经尝试了所有在搜索时找到的 str_replace 函数 -

1) var re = /(?![\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3})./g;
   params.body_html = html.replace(re, '');
   angular.toJson(params); // gives error


2) params.body_html.replace(/\uFFFD/g, '');
   angular.toJson(params); // gives error

我不知道这是什么字符(可能是 unicode)。当我将其复制到 emacs 文件时,它被视为 �????

注意:当您编辑此问题并单击edit the snippet 以获取上述html 时,您会看到此字符为红点。

关于我如何完成这项工作的任何提示/想法?

【问题讨论】:

  • 它是that character
  • 它对我来说很好,请参阅JS Fiddle
  • +1 用于 js fiddle,谢谢!但我确信它只是因为这个而失败,因为当我使用普通文本“Some Text”而不是这个时,它工作正常。知道如何修复它吗?
  • 请提供一个无法正常工作的 JS Fiddle。
  • 它对我不起作用,因为我将所有 html 作为字符串放入 body_html 中,并且此行分隔符对字符串有一些语法问题。不过不确定。

标签: javascript jquery angularjs json unicode


【解决方案1】:

得到这个工作:

params.body_html = params.body_html.replace(/\u2028/g, '');
angular.toJson(params); //works fine.

感谢@Gothdo 提供角色链接。

但问题是它只会在 html 只有这个特定的 unicode 字符时替换。是否有任何函数可以替换或修剪所有 unicode 字符?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 2013-10-16
    • 2014-06-09
    • 2014-06-14
    • 1970-01-01
    相关资源
    最近更新 更多