-1

我需要去掉字符串的某些元素。

字符串看起来有点像这样:

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>&nbsp;</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
4

2 回答 2

1

我将采取的步骤如下:

  1. 查找子字符串的索引<b>Frame</b>:
  2. 查找下一个索引<br>
  3. 从第一个索引到第二个索引加上4(的长度<br>)切出一个子串
  4. 使用正则表达式去除 html 标签
  5. 重复第二行(镜头)的过程
于 2013-01-09T04:49:48.437 回答
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);
});
于 2013-01-14T22:45:42.943 回答