我有一个像这样的字符串:
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>"]
感谢您的帮助。 问候, 肯
答案 0 :(得分: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)
答案 1 :(得分: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
编辑。
答案 2 :(得分: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 - split without losing the separator