Javascript 使用$.each解析字符串中的unicode

Javascript 使用$.each解析字符串中的unicode,javascript,jquery,Javascript,Jquery,我正试图从MouseMove事件(工具提示)中过滤中的字符串。 过滤后的字符串需要显示为文本 问题是字符串看起来像: 需要筛选此字符串\r\n此字符串\u00EB中还有unicode 我想要什么: 需要筛选此字符串此字符串中还有unicode: HTML如下所示: <img onmousemove="showInfo(event,'This string needs to be filtered. \r\n There is also unicode in this string: \u0

我正试图从MouseMove事件(工具提示)中过滤
中的字符串。
过滤后的字符串需要显示为文本

问题是字符串看起来像:
需要筛选此字符串\r\n此字符串\u00EB中还有unicode

我想要什么:
需要筛选此字符串
此字符串中还有unicode:

HTML如下所示:

<img onmousemove="showInfo(event,'This string needs to be filtered. \r\n There is also unicode in this string: \u00EB.');" onmouseout="hideInfo();" />

这就是我所尝试的:

$(document).ready(function() {
    $('td > img').each(function() {
        var toolTip = $(this).attr('onmousemove'),
            comment = toolTip.match(/([\'])(\\?.)*?\1/),
            parentCell = $(this).parent();

        $("div.timelineRow").css("padding", "7px");
        $("<td><b>Info:</b><span> " + comment[0] + "</span></td>").insertAfter(parentCell);
        $(this).insertAfter(parentCell);
    });
});
$(文档).ready(函数(){
$('td>img')。每个(函数(){
var toolTip=$(this.attr('onmousemove'),
comment=toolTip.match(/([\'])(\\?)*?\1/),
parentCell=$(this.parent();
$(“div.timelineRow”).css(“填充”、“7px”);
$(“信息:+注释[0]+”)。插入后面(parentCell);
$(this).insertAfter(parentCell);
});
});

尝试使用JSON.parse对Unicode字符进行解码。(注意双引号中的换行符使其成为有效的JSON)

然后用

标记替换新行,将它们转换为HTML换行符元素(浏览器不会呈现
\r\n

e、 g

var htmlComment=JSON.parse(“'+comment[0]+”).replace(“\r\n”,“
”); $(“信息:+htmlComment+”)。插入后面(parentCell);
代码中的“showInfo”功能在哪里?非常好用!非常感谢。也许值得一提的是,如果您这样做的唯一原因是出于显示目的,那么您可能只需要使用CSS属性。例如:
var htmlComment = JSON.parse('"' + comment[0] + '"').replace("\r\n", "<br>");
$("<td><b>Info:</b><span> " + htmlComment + "</span></td>").insertAfter(parentCell);