【问题标题】:HTML string remove extra spaces with the help of javascriptHTML字符串在javascript的帮助下删除多余的空格
【发布时间】:2017-12-04 02:42:49
【问题描述】:

您好,我有一个类似

的 HTML 字符串
"     <div>       <p>You have received an alert from         project <span class="fields"          field="template.variable.ProjectName">          Project Name</span>        <br />       </p>        <p>        <span          field="template.variable.AlertName"          class="fields">Alert Name</span> <span          field="template.variable.AlertName"          class="fields">Alert Name</span> <span          field="template.variable.AlertName"          class="fields">Alert Name</span>        <br />       </p>        <p>Follow these steps to access the      alert:</p>        <ol>         <li>Log into your  for          Contract Management project         at <span            field="template.variable.AlertProjectLink"            class="fields">Alert Project Link</span></li>          <li>If necessary, enter your email address and the password          that you created when you completed your         registration.</li>          <li>Go to the Alerts tab to see your          unread alerts and         access links to the documents.</li>      </ol>        <p>        <span class="fields"          field="template.variable.AlertDocumentList">          Alert Document List</span></p>        <p>Please do not reply        to this message</p>        <p>.Space2 space gaurav Replies are        routed to an unmonitored       mailbox.</p>        <p>If you        would like additional assistance, please contact       Merrill        Technical Support, available 24 hours a day, seven       days a      week.</p>        <p>        <span class="template"          field="template.variable.ImportTemplate"          template="template.types.TechSupportContactInformation">          Tech Support Contact Information</span> test</p>        <p>Testing is going on</p>     </div>   "

我需要用单个空格而不是标签内的所有连续空格替换,就像你可以看到我有&lt;span field ="template.variable.AlertName" class="fields"&gt; Alert Name&lt;/span&gt;

我需要&lt;span field="template.variable.AlertName" class="fields"&gt;Alert Name&lt;/span&gt;

如你所见,我不想触及 HTML 标签属性的空格。

任何帮助将不胜感激。

*仅限JAVASCRIPT

【问题讨论】:

  • @BalajMarius 不起作用
  • 一个或多个空格和换行符在 HTML 中被解释为一个空格,那么有什么意义呢?
  • 仅供参考,如果您删除 pre 标签之间的空格,您将被破坏。

标签: javascript string replace space


【解决方案1】:

使用正则表达式

/(<.*?>)|\s+/g

replace

正则表达式将匹配

  1. (&lt;.*?&gt;): HTML 标记并将其添加到第一个捕获组中($1 替换)
  2. |: 正则表达式中的或
  3. \s+: 或者一个或多个空格

替换时,检查是否是HTML标签,否则替换多余的空格。

$1 是第一个捕获的组,即 HTML 标记。如果存在标签,则不执行任何操作,否则删除多余的空格。

var html = `     <div>       <p>You have received an alert from         project <span class="fields"          field="template.variable.ProjectName">          Project Name</span>        <br />       </p>        <p>        <span          field="template.variable.AlertName"          class="fields">Alert Name</span> <span          field="template.variable.AlertName"          class="fields">Alert Name</span> <span          field="template.variable.AlertName"          class="fields">Alert Name</span>        <br />       </p>        <p>Follow these steps to access the      alert:</p>        <ol>         <li>Log into your  for          Contract Management project         at <span            field="template.variable.AlertProjectLink"            class="fields">Alert Project Link</span></li>          <li>If necessary, enter your email address and the password          that you created when you completed your         registration.</li>          <li>Go to the Alerts tab to see your          unread alerts and         access links to the documents.</li>      </ol>        <p>        <span class="fields"          field="template.variable.AlertDocumentList">          Alert Document List</span></p>        <p>Please do not reply        to this message</p>        <p>.Space2 space gaurav Replies are        routed to an unmonitored       mailbox.</p>        <p>If you        would like additional assistance, please contact       Merrill        Technical Support, available 24 hours a day, seven       days a      week.</p>        <p>        <span class="template"          field="template.variable.ImportTemplate"          template="template.types.TechSupportContactInformation">          Tech Support Contact Information</span> test</p>        <p>Testing is going on</p>     </div>   `;

var res = html.replace(/(<.*?>)|\s+/g, (m, $1) => $1 ? $1 : ' ');
console.log(res);

【讨论】:

  • 它工作正常谢谢你的回答,但你能解释一下吗
  • 标签之间没有多余的空格`

    你有`

  • @mattn 不多,一个空格。这就是OP想要的。标签之外的单个空格而不是多个。
  • 啊,我明白 OP 现在想要什么了。对不起。
猜你喜欢
相关资源
最近更新 更多
热门标签