【发布时间】:2015-12-05 14:05:21
【问题描述】:
我想删除此字符串中class 属性末尾的空格:
<div class="test wrapper " id="test"> " sample text " </div>
如果我使用
text = text.replace(/\s+"/g, '"');
那么sample text 后面的空格也将被删除,我想保留那个空格。
你知道我该怎么做吗?
【问题讨论】:
-
一般来说,你应该用正则表达式来做。在这种特定情况下,您可以替换
replace(/\s+">/g, '">');。 -
@ndn 类可能不是最后一个属性,那么您知道我该怎么做吗?
-
replace(/\s+"([^>]*)>/g, '"$1>');,但还是要使用 html 解析器。
标签: javascript regex html-parsing