去除字符串的某些元素

时间:2013-01-09 04:45:03

标签: javascript jquery string parsing

我需要删除字符串的某些元素。

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

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

2 个答案:

答案 0 :(得分:1)

我将采取的步骤如下:

  1. 查找子字符串<b>Frame</b>:
  2. 的索引
  3. 找到下一个<br>
  4. 的索引
  5. 从第一个索引切换到第二个索引的子字符串加上4(<br>的长度)
  6. 使用regex
  7. 剥离html标记
  8. 重复第二行(镜头)
  9. 的过程

答案 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);
});