我有一根绳子,就像
Lorem Ipsum is simply dummy text of the printing and typesetting
industry.<grab> First Item</grab>Lorem Ipsum is simply dummy text of
the printing and typesetting industry.<grab>Second Item</grab>Lorem
Ipsum is simply dummy text of the printing and typesetting
industry.<grab>Third Item</grab>Lorem Ipsum is simply dummy text of
the printing and typesetting industry.现在,我需要获取<grab>....</grab>标记之间的所有文本,并添加两个数组。我试过了,但没有通过
var data = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.<grab> First Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<grab>Second Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<grab>Third Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.';
var array = $('<grab>').map(function() {
return $(this).text();
}).get();
$('body').append(array);<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
你能告诉我怎么做吗?
发布于 2016-06-17 17:02:47
这些实际上不是DOM元素,因此regex可能会对您有所帮助。
var data = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.<grab> First Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<grab>Second Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<grab>Third Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.';
var r = /<grab>(.*?)<\/grab>/g;
var grabs = data.match(r).map(function(x){
return x.replace(r,'$1');
});
console.log(grabs);
发布于 2016-06-17 17:27:09
尽管正则表达式是一个选项,但最简单的方法是简单地使用DOM解析(元素类型是自定义还是其他类型似乎并不重要):
var data = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry <grab> First Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<grab>Second Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<grab>Third Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
// creating a <div> element with the data string
// as its innerHTML:
elem = $('<div />',{
'html' : data
}),
// using find() to retrieve the <grab> elements from
// within the newly-created <div>, and using map()
array = elem.find('grab').map(function () {
// ...to return the trimmed text of each
// found element:
return this.textContent.trim();
// converting the map to an Array:
}).get();
// appending the joined array (joining the array together
// with a comma-white-space sequence) to the <body>:
$('body').append(array.join(', '));
// => First Item, Second Item, Third Item
var data = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry <grab> First Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<grab>Second Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<grab>Third Item</grab>Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
elem = $('<div />',{
'html' : data
}),
array = elem.find('grab').map(function () {
return this.textContent.trim();
}).get();
$('body').append(array.join(', '));<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
https://stackoverflow.com/questions/37886767
复制相似问题