简体   繁体   English

使用javascript获取标签之间的文本

[英]Get text between tags using javascript

I am trying to get prices from between span tags.我试图从跨度标签之间获取价格。 I would like to have all prices in an array.我想将所有价格都放在一个数组中。 I cant seem to get it to work, I am guessing my regex is incorrect.我似乎无法让它工作,我猜我的正则表达式不正确。

I am looking for any span tags with the class 'amount', the tag has no other attributes set and only has one class.我正在寻找具有“数量”类的任何跨度标签,该标签没有设置其他属性并且只有一个类。 Eg <span class="amount">&pound;9.99</span>例如<span class="amount">&pound;9.99</span>

var prices = resp.fragments['data'].match(/<span class=\"amount\">(.*?)<\/span>/g)
    .map(function(val){
      return val;
});

Output输出

[ '&pound;9.99', '&pound;100.00' ]

I am trying to get prices from between span tags.我试图从跨度标签之间获取价格。 I would like to have all prices in an array.我想将所有价格都放在一个数组中。 I cant seem to get it to work, I am guessing my regex is incorrect.我似乎无法让它工作,我猜我的正则表达式不正确。

I am looking for any span tags with the class 'amount', the tag has no other attributes set and only has one class.我正在寻找具有“数量”类的任何跨度标签,该标签没有设置其他属性并且只有一个类。 Eg <span class="amount">&pound;9.99</span>例如<span class="amount">&pound;9.99</span>

var prices = resp.fragments['data'].match(/<span class=\"amount\">(.*?)<\/span>/g)
    .map(function(val){
      return val;
});

Output输出

[ '&pound;9.99', '&pound;100.00' ]

* UPDATE * * 更新 *

Turns out it was an encoding with the ajax response resp.fragments['data'] .原来这是一个带有 ajax 响应resp.fragments['data']的编码。

I was using regex as it is something I have not really used before in JS and thought I would have a play.我正在使用正则表达式,因为它是我之前在 JS 中没有真正使用过的东西,我想我会玩。 I did look at many examples and after about 45 mins with no success I thought a fresh set of eyes would fix it.我确实看过很多例子,在大约 45 分钟没有成功之后,我认为一组新的眼睛会修复它。

@spaceman Thanks for the helpful comment. @spaceman 感谢您的有用评论。 Your one of those people if someone asked "Is there is a doctor in the house?", you would stand up and say "Sweet load there are loads of doctors out there".如果有人问“房子里有医生吗?”,你就是其中的一个,你会站起来说“甜蜜的负担,外面有很多医生”。

While a regular expression could work for this, it might be easier to simply select the <span class='amount'> elements and map their innerHTML content to an array via the map() function:虽然正则表达式可以解决这个问题,但简单地选择<span class='amount'>元素并通过map()函数将它们的innerHTML内容映射到数组可能更容易:

// This would yield an array containing your values
var amounts = Array.prototype.slice
                             .call(document.querySelectorAll('span.amount'))
                             .map(function(a){ return a.innerHTML; });

You can see a working example of this demonstrated here .您可以在此处查看演示的工作示例

You can use this instead.您可以改用它。

 var prices = document.getElementsByClassName('amount'); var price_array = []; for (i= 0; i < prices.length; ++i) { price_array.push(prices[i].innerHTML); } document.write(" | " + price_array);
 <span class='amount'>&pound;123</span> <span class='amount'>&pound;3</span> <span class='amount'>&pound;5</span> <span class='amount'>&pound;64</span>

You don't need to use regex or jQuery for this.您不需要为此使用正则表达式或 jQuery。

Simplest method will be to add this to an invisible DOM object and then traverse it via DOM API最简单的方法是将它添加到一个不可见的 DOM 对象,然后通过 DOM API 遍历它

var text  = '<span class="amount">&pound;9.99</span><span class="amount">&pound;9.99</span>'

//now append it to an DOM object //现在将它附加到一个DOM对象

var wrapperDiv = "<div style='display:none' id='tmpDiv'>" + text + "</div>";
document.body.innerHTML += wrapperDiv;

var elements = document.querySelectorAll( "#tmpDiv amount" );
var output = Array.prototype.slice.call( elements ).map( function(val){
  return val.innerText;
})

Another approach could be split the text by <span class="amount"> and get the value after first index另一种方法可以通过<span class="amount"> split文本并在第一个索引后获取值

DEMO演示

 var text = '<span class="amount">&pound;9.99</span><span class="amount">&pound;9.99</span>' var output = []; text.split('<span class="amount">').forEach( function(val, index) { if (index > 0 ) { output.push( val.replace( "</span>", "" ) ); } }); document.body.innerHTML += JSON.stringify( output, 0, 4 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM