最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: How to get array of strings of text within p tags - Stack Overflow

programmeradmin3浏览0评论

Lets say I have string with a lot of p tags in it...

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

..how do I get an array, each item in the array is a string of text that was in the p tags:

 [
    "Some text.",
    "Some more. Some more text.",
    "And even some more text."
]

One way I suppose would be to get rid of the p tags...

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

..And then use .split() to get out each sentence. But I don't really want to get out each sentence, just text w.in p tags

var stringAsArray = stringWithOutTags.split(".");

Lets say I have string with a lot of p tags in it...

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

..how do I get an array, each item in the array is a string of text that was in the p tags:

 [
    "Some text.",
    "Some more. Some more text.",
    "And even some more text."
]

One way I suppose would be to get rid of the p tags...

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

..And then use .split() to get out each sentence. But I don't really want to get out each sentence, just text w.in p tags

var stringAsArray = stringWithOutTags.split(".");
Share Improve this question asked May 25, 2016 at 23:08 NewToJSNewToJS 2,1014 gold badges37 silver badges67 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

If you are executing the code on browser, you can parse the string as HTML instead of using regular expression:

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

You can omit the <p> tags from your string and split using the closing </p> tag only to get the desired result.

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

Why not split after you replace:

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('  ');

https://jsbin./wopute/1/edit?js,console

Note: please only use this method if you are sure that you can trust the input string (i.e. it is not user input)!

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;
});

Return an array of strings that wraps each of the original strings in an HTML-like <p></p> tag.

const names = ["alice", "bob", "charlie", "danielle"]

const wrapped = names.map( (name) => {
    return `<p>${name}</p>`
} )

console.log(wrapped)
发布评论

评论列表(0)

  1. 暂无评论