jQuery以字符串格式在标记内部抓取文本并添加到数组

我有字符串就像

Lorem Ipsum is simply dummy text of the printing and typesetting industry. First ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.Second ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.Third ItemLorem Ipsum is simply dummy text of the printing and typesetting industry. 

现在我需要抓住....标签之间的所有文本,并将它们添加到一个数组中。 我试过这个,但它没有通过

 var data = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. First ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.Second ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.Third ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.'; var array = $('').map(function() { return $(this).text(); }).get(); $('body').append(array); 
  

你能告诉我怎么做吗?

实际上这些不是DOM元素,因此正则表达式可能对你有帮助。

 var data = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. First ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.Second ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.Third ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.'; var r = /(.*?)<\/grab>/g; var grabs = data.match(r).map(function(x){ return x.replace(r,'$1'); }); console.log(grabs); 

尽管正则表达式是一个选项,最简单的方法是简单地使用DOM解析(如果元素类型是自定义的或其他方式似乎并不重要):

 var data = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry  First ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.Second ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.Third ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.', // creating a 
element with the data string // as its innerHTML: elem = $('
',{ 'html' : data }), // using find() to retrieve the elements from // within the newly-created
, 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').append(array.join(', ')); // => First Item, Second Item, Third Item
 var data = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry  First ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.Second ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.Third ItemLorem Ipsum is simply dummy text of the printing and typesetting industry.', elem = $('
',{ 'html' : data }), array = elem.find('grab').map(function () { return this.textContent.trim(); }).get(); $('body').append(array.join(', '));