1

我有一个这样的字符串

var My_string = "<div id='some_id_1'>Some Div Text 1</div><div id='some_id_2'>Some Div Text 2</div><div id='some_id_3'>Some Div Text 3</div><div id='some_id_4'>Some Div Text 4</div>";

我想搜索字符串并将找到的每个完整 div 添加到数组中。

所以我想在字符串中搜索

"<div" 

接着:

"</div>" 

并抓住中间的文字:

var My_Div_Value = "<div id='some_id_1'>Some Div Text 1</div>";

并将每个推入一个数组:

My_Divs_Array.push(My_Div_Value);

所以 My_Divs_Array 将有:

["<div id='some_id_1'>Some Div Text 1</div>", "<div id='some_id_2'>Some Div Text 2</div>", "<div id='some_id_3'>Some Div Text 3</div>", "<div id='some_id_4'>Some Div Text 4</div>"]

我只是不确定解决这个问题的最佳方法。我想象使用“ IndexOf() ”或者我不确定。我只想问专业人士实现这一目标的最佳方法是什么。


从最初的回复来看 - 我不确定我的帖子是否被理解。我可以很容易地使用字符串示例:

var My_string = "<foo>everything in between</bar><foo>everything in between</bar><foo>everything in between</bar><foo>everything in between</bar>";

"<foo>"我需要将每个++实例单独收集并存储到一个数组中"everything in between""</bar>""<foo>everything in between</bar>"

["<foo>everything in between</bar>", "<foo>everything in between</bar>", "<foo>everything in between</bar>", "<foo>everything in between</bar>"]

谢谢你的帮助。问候,肯

4

3 回答 3

2

改用 DOM。

var My_string = "<div id='some_id_1'>Some Div Text 1</div><div id='some_id_2'>Some Div Text 2</div><div id='some_id_3'>Some Div Text 3</div><div id='some_id_4'>Some Div Text 4</div>";

var wrapper = document.createElement('div');
wrapper.innerHTML = My_string;

console.log(wrapper.getElementsByTagName('div')); // get all the div
console.log(wrapper.getElementById('some_id_1')); // if you know the id

或者使用 jQuery。

$(My_string)
于 2012-10-04T04:04:40.820 回答
0

要添加到 xdazz 的回复中:

var My_string = "<div id='some_id_1'>Some Div Text 1</div><div id='some_id_2'>Some Div Text 2</div><div id='some_id_3'>Some Div Text 3</div><div id='some_id_4'>Some Div Text 4</div>";

var wrapper = document.createElement('div'); Creates a DOM object.
wrapper.innerHTML = My_string; // Loads the string into DOM as HTML.
var objs = wrapper.getElementsByTagName('div'); // Get all the objects (each div tag element).

var My_Divs_Array; // Declare your output array.
for (var i = 0; i < objs.length; i++) { // Iterate through array of div elements.
    My_Divs_Array.push(objs[i].outerHTML); // Push the entire text portion of the div elements into array My_Divs_Array.
}
// Do what you want with My_Divs_Array, which now holds the strings that you want.

你不需要对网页本身做任何事情。您只使用 DOM 及其 HTML 元素解析方法来处理您的字符串。

查看:https ://developer.mozilla.org/en-US/docs/DOM/element.outerHTML

已编辑。

于 2012-10-04T04:21:18.367 回答
0
var str = "<div id='some_id_1'>Some Div Text 1</div><div id='some_id_2'>Some Div Text 2</div><div id='some_id_3'>Some Div Text 3</div><div id='some_id_4'>Some Div Text 4</div>";

var Array_Of_Divs = str.split(/(?=<div )/);

alert(Array_Of_Divs);

产生:

["<div id='some_id_1'>Some Div Text 1</div>", "<div id='some_id_2'>Some Div Text 2</div>", "<div id='some_id_3'>Some Div Text 3</div>", "<div id='some_id_4'>Some Div Text 4</div>"];

正是我要的:)

非常感谢:Namida Aneskans https://stackoverflow.com/users/1662018/namida-aneskans javascript - 拆分而不丢失分隔符

于 2012-10-04T07:26:03.317 回答