Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:如何获取p标记中的文本字符串数组_Javascript_Arrays_String - Fatal编程技术网

Javascript:如何获取p标记中的文本字符串数组

Javascript:如何获取p标记中的文本字符串数组,javascript,arrays,string,Javascript,Arrays,String,假设我的字符串中有很多p标签 var myString = "<p>Some text.</p><p>Some more. Some more text.</p><p>And even some more text.</p>"; 我想有一种方法是去掉p标签 var stringWithOutTags = myString.replace(/(<p>|<\/p>)/g, " "); 替换后为什

假设我的字符串中有很多p标签

var myString = "<p>Some text.</p><p>Some more. Some more text.</p><p>And even some more text.</p>";
我想有一种方法是去掉p标签

   var stringWithOutTags = myString.replace(/(<p>|<\/p>)/g, " ");

替换后为什么不拆分:

var a = "<p>Some text.</p><p>Some more. Some more text.</p><p>And even some more text.</p>";

var b = a.replace(/(<p>|<\/p>)/g, " ").split('  ');
var a=“一些文本。

更多。更多文本。

甚至更多文本。

”; 变量b=a.replace(/()/g,”).split(“”);

更换后为什么不拆分:

var a = "<p>Some text.</p><p>Some more. Some more text.</p><p>And even some more text.</p>";

var b = a.replace(/(<p>|<\/p>)/g, " ").split('  ');
var a=“一些文本。

更多。更多文本。

甚至更多文本。

”; 变量b=a.replace(/()/g,”).split(“”);

您可以省略字符串中的标记,并仅使用结束标记进行拆分,以获得所需的结果

myString.replace('<p>', '').split('</p>');
myString.replace(“”,“).split(“

”);
您可以从字符串中省略标记,并仅使用结束标记进行拆分,以获得所需的结果

myString.replace('<p>', '').split('</p>');
myString.replace(“”,“).split(“

”);
如果在浏览器上执行代码,则可以将字符串解析为HTML而不是使用正则表达式:

var el = document.createElement('div');
el.innerHTML = myString;
var texts = [].map.call(el.querySelectorAll('p'), function(p) {
   return p.textContent;
});

如果在浏览器上执行代码,则可以将字符串解析为HTML而不是使用正则表达式:

var el = document.createElement('div');
el.innerHTML = myString;
var texts = [].map.call(el.querySelectorAll('p'), function(p) {
   return p.textContent;
});

注意:如果您确信可以信任输入字符串(即,它不是用户输入),请仅使用此方法

var myString = "<p>Some text.</p><p>Some more. Some more text.</p><p>And even some more text.</p>";

// Create a "div" element
var div = document.createElement("div");

// Get browser to parse string, and set the parsed HTML elements as
// the contents of the div element
div.innerHTML = myString;

// Loop over the child elements of the div, and return an array of
// the textual content the elements. If you wish to preserve tags
// inside the <p> elements, replace .textContent with .innerHTML
var arrayOfStrings = Array.prototype.map.call(div.childNodes, function (pTag) {
    return pTag.textContent;
});
var myString=“一些文本。

更多。更多文本。

甚至更多文本。

”; //创建一个“div”元素 var div=document.createElement(“div”); //让浏览器解析字符串,并将解析后的HTML元素设置为 //div元素的内容 div.innerHTML=myString; //循环div的子元素,并返回 //文本内容包含元素。如果您希望保留标签 //在元素中,将.textContent替换为.innerHTML var arrayOfStrings=Array.prototype.map.call(div.childNodes,函数(pTag){ 返回pTag.textContent; });
注意:如果您确信可以信任输入字符串(即,它不是用户输入),请仅使用此方法

var myString = "<p>Some text.</p><p>Some more. Some more text.</p><p>And even some more text.</p>";

// Create a "div" element
var div = document.createElement("div");

// Get browser to parse string, and set the parsed HTML elements as
// the contents of the div element
div.innerHTML = myString;

// Loop over the child elements of the div, and return an array of
// the textual content the elements. If you wish to preserve tags
// inside the <p> elements, replace .textContent with .innerHTML
var arrayOfStrings = Array.prototype.map.call(div.childNodes, function (pTag) {
    return pTag.textContent;
});
var myString=“一些文本。

更多。更多文本。

甚至更多文本。

”; //创建一个“div”元素 var div=document.createElement(“div”); //让浏览器解析字符串,并将解析后的HTML元素设置为 //div元素的内容 div.innerHTML=myString; //循环div的子元素,并返回 //文本内容包含元素。如果您希望保留标签 //在元素中,将.textContent替换为.innerHTML var arrayOfStrings=Array.prototype.map.call(div.childNodes,函数(pTag){ 返回pTag.textContent; });
Great point,如果输入是用户输入,那么字符串不应该被html解析,至少不应该以这种乐观的方式。我的回答没有提到这种方法涉及的安全问题。很好的一点是,如果输入是用户输入,那么字符串不应该被html解析,至少不应该以这种乐观的方式。我的回答没有提到这种方法涉及的安全问题。请在回答中对代码添加一些描述。好主意,因为这是SSR保存。就个人而言,我更喜欢使用“全局”方法:myString.replace(//g),).split(“

”)请在答案中对代码添加一些描述。好主意,因为这是SSR保存。就个人而言,我更喜欢使用“全局”方法:myString.replace(//g),).split(“

”)