[英]HTML string remove extra spaces with the help of javascript
Hi I have a HTML string like 嗨,我有一个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> "
I need to replace all the consecutive spaces with single Space but not within tags, Like you can see that I Have <span field ="template.variable.AlertName" class="fields"> Alert Name</span>
我需要用单个空格替换所有连续空格但不能用标签替换,就像你可以看到我有
<span field ="template.variable.AlertName" class="fields"> Alert Name</span>
I need <span field="template.variable.AlertName" class="fields">Alert Name</span>
我需要
<span field="template.variable.AlertName" class="fields">Alert Name</span>
As you can see that I don't want to touch spaces of the attributes of HTML tag. 正如您所看到的,我不想触摸HTML标记属性的空格。
Any help would be highly Appreciated Thanks. 任何帮助都将得到高度赞赏谢谢。
*JAVASCRIPT ONLY *仅限JAVASCRIPT
Use the regex 使用正则表达式
/(<.*?>)|\s+/g
with replace
. 与
replace
。
The regex will match 正则表达式将匹配
(<.*?>)
: HTML tag and add it in first capturing group( $1
in replace) (<.*?>)
:HTML标记并将其添加到第一个捕获组中(替换中$1
) |
: OR in regex \\s+
: Or one or more spaces \\s+
:或一个或多个空格 In replacement, check if it is HTML tag else, replace extra spaces. 在替换中,检查它是否是HTML标记,替换额外的空格。
$1
is first captured group ie HTML tag. $1
是首先捕获的组,即HTML标记。 If tag is present, then don't do anything, else remove the extra spaces. 如果存在标记,则不执行任何操作,否则删除多余的空格。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.