电子邮件标题在“&"时无法正确显示在标题中,如何在JavaScript中修复? [英] Email titles not displaying correctly when '&' is in the title, how to fix in JavaScript?

查看:103
本文介绍了电子邮件标题在“&"时无法正确显示在标题中,如何在JavaScript中修复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码以下列格式显示文章标题,简短描述和作者姓名的列表:

I have some code that displays the list of article titles, their short descriptions and the authors' name in the following format:

标题(作者姓名)
说明

Title (authors' name)
description

作者的姓名和描述与此处无关,因为它们始终正确显示.大多数标题也可以正确显示,下面是一些示例:

Author's names and descriptions are not relevant here because they always display correctly. Most of the titles display correctly too, here are some made up examples:

您需要了解的关于银行业务的最重要的一件事(作者的名字)-正确显示

The Single Most Important Thing You Need To Know About Banking (authors' name) - displays correctly

电源和功率如何实用工具使我成为更好的销售员(作者的名字)-正确显示

How Power & Utilities Made Me A Better Salesperson (authors' name) - displays correctly

PG& E和查克·诺里斯效应(作者的名字)-以下列方式显示不正确: & E和查克·诺里斯效应(作者的名字)

PG&E And The Chuck Norris Effect (authors' name) - displays incorrectly in the following way: &E And The Chuck Norris Effect (authors' name)

仅显示此单个示例存在问题.这就是为什么我专注于&"象征.但是&"在其他标题中,&"似乎不是问题前后都有一个空格.

There is a problem only with displaying this single example. That is why I focused on '&' symbol. But '&' doesn't seem to be the problem in other titles where '&' has a space before and after it.

我解决此问题的代码是这样的,但它不会以任何方式影响输出...

My code to fix this issue is like this but it doesn't affect the output in any way...

// this is not my code
offerRep += '<a _urltype="11" href="' + q.docUrl + '" alt="' + q.documentTitle +'" >' + q.documentTitle + ' "' + fullSubTitle + '"' + ' (' + analystName[0] + ')</a>';

// this is my code 
if (q.documentTitle.indexOf('&') > -1) {
  q.documentTitle.replace(/'&'/g, '&amp;');
} else {
  return q.documentTitle;
}

推荐答案

第一个问题-可能是因为您仅发布了部分代码(*)-此行:

First problem - which maybe because you only posted part of the code (*) - this line:

q.documentTitle.replace(/'&'/g, '&amp;');

从不做任何事情,因为replace返回一个新字符串,您不会以任何方式返回或使用它.

never does anything, because replace returns a new string which you are not returning or using in any way.

然后,正则表达式/'&'/表示您要查找的是撇号,后跟&"号,再后面是撇号.您只需要/&/.

Then the regular expression /'&'/ means you are looking for a apostrophe followed by a ampersand followed by an apostrophe. You just need /&/.

ifindexOf中使用if并不是真正有用的.如果replace找不到正则表达式,它将完全不执行任何操作,因此请删除if.

Using if with indexOf isn't really useful here. If replace doesn't find the regular expression it will simply do nothing so drop the if.

通常,仅仅用这样的未知外部内容盲目构建HTML是一个非常糟糕的主意,不仅需要转义&.看看:我可以在javascript中转义html特殊字符吗?

Generally it's a very bad idea just to blindly build HTML with unknown external content like this and it's not only the & that needs escaping. Have a look at: Can I escape html special chars in javascript?

使用常规功能,并转义要插入HTML的 ALL 外部数据:

Use a general function and escape ALL external data you want to insert into the HTML:

function escapeHtml(unsafe) {
  return unsafe
     .replace(/&/g, "&amp;")
     .replace(/</g, "&lt;")
     .replace(/>/g, "&gt;")
     .replace(/"/g, "&quot;")
     .replace(/'/g, "&#039;");
 }


offerRep += '<a _urltype="11" href="' + escapeHtml(q.docUrl) + 
            '" alt="' + escapeHtml(q.documentTitle) +'" >' + 
            escapeHtml(q.documentTitle) + ' "' + escapeHtml(fullSubTitle) + '"' + 
            ' (' + escapeHtml(analystName[0]) + ')</a>';

评论中Salman A的建议也是一种好方法,因为您无需记住"逃生的方式和方式.

Salman A's suggestion in the comments is also a good way, because you don't need to "remember" how and what to escape.

一种更安全,更好和更现代的方法是使用模板引擎.例如 Handlebars.js (但还有很多):

A safer, better and more modern way would be to use a template engine. For example Handlebars.js (but there are many many more):

var linkTemplate = Handlebars.compile('<a _urltype="11" href="{{url}}" alt="{{title}}">{{title}} "{{subtitle}}" ({{analystname}})</a>');

var linkHtml = linkTemplate({
                 url: q.docUrl,
                 title: q.documentTitle,
                 subtitle: fullSubTitle,
                 analystname: analystName[0]
               });

offerRep += linkHtml;

(*)在用代码提问时,请始终在有用的上下文中发布代码,绝不要只有几行代码不能自己发挥作用.

(*) When asking questions with code ALWAYS post the code with useful context, never just a few lines that don't work on their own.

这篇关于电子邮件标题在“&amp;"时无法正确显示在标题中,如何在JavaScript中修复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆