我需要删除字符串的某些元素。
字符串看起来有点像这样:
var htmlelement = '<b>Frame</b>: KHAKI TORT / MATTE BLACK<br>
<b>Lens</b>: <span class="titlecaps">SMOKE MONO</span><br>
<b>Protection</b>: Category 3 lens - Maximum UV protection
<div><b>Hardware</b>: Initial temple badge<br>
<b>Packaging</b>: Le Specs protective pouch included<br>
<b>Gender</b>: Men
<div><b>Model</b>: 1202055<br>
<b>Warranty</b>: Le Specs are warranted for one year against manufacturing faults from the date of purchase.<br>
<br>
<hr style="border:dotted #DDD; border-width:1px 0 0 0; height:0;line-height:0px;font-size:0;margin:0;padding:10px;">
<span style="font-family:times new roman;"><span style="font-weight:bold">DESIGNER NOTES</span><br>
<br>
This oversize and tough wayfarer style in khaki tortoiseshell features a contrast black top bar.
</span>
</div>
</div>'
因为我已经从HTML元素转换它们。
我需要删除的是镜头和框架,所以对于上面的字符串,我需要返回:
Frame: KHAKI TORT / MATTE BLACK
Lens: SMOKE MONO
为了解决问题,字符串可能并不总是完全如所示,例如,初始字符串可能看起来像这样(以不同的顺序):
<b>Model</b>: 1202080<br>
<b>Protection</b>: Category 3 lens - Maximum UV protection
<div><b>Hardware</b><span>:</span><b> </b><span>Silver logo temple badge</span><br>
<b>Packaging</b>: Le Specs protective pouch included<br>
<b>Gender</b>: Women<br>
<b>Warranty</b>: Le Specs are warranted for one year against manufacturing faults from the date of purchase.<br>
<br>
<b>Frame</b>: <span style="titlecaps">BLACK PRINT</span><br>
<hr style="border:dotted #DDD; border-width:1px 0 0 0; height:0;line-height:0px;font-size:0;margin:0;padding:10px;">
<b>Lens</b>: <span class="titlecaps">SMOKE GRAD</span><br>
<span style="font-family:times new roman;"><span style="font-weight:bold">DESIGNER NOTES</span><br>
<br>
Bold and glamorous, this oversize square style features soft curves featuring signature Le Specs Aztec print.
</span>
</div>
我仍然需要简单地回复:
Frame: BLACK PRINT
Lens: SMOKE GRAD
答案 0 :(得分:1)
我将采取的步骤如下:
<b>Frame</b>:
<br>
<br>
的长度)答案 1 :(得分:0)
我最终使用了Wes C建议的内容,
这是我完成时的样子:
$.each($(".descholder"), function(){
var originalVal = $(this).html();
var firstVal = originalVal.indexOf("Frame");
var secondVal = originalVal.indexOf("<br>");
var lensResult = originalVal.substring(firstVal, secondVal);
var regex = /(<([^>]+)>)/ig;
var body = lensResult;
var result = body.replace(regex, "");
result = result.replace("Frame: ", '');
$(this).parent('.descresult').html(result);
});