I receive data in the following format from the server
<ul>
<li>Some text</li>
<li>Other text</li>
</ul>
Basically string containing a standard unordered list. What I want to do now is to remove the ul
tags that wrap the list items, so that I would only receive the inner content. Like this:
<li>Some text</li>
<li>Other text</li>
It might be worth mentioning that ul
tags will always be present when I get the data so I don't realy care whats inside it, or are there any more ul
tags nested within. I just need to remove the outer ones and get the data inside.
Either a JavaScript or PHP solution would be welcomed.
var div = document.createElement("div");
div.innerHTML = "<ul><li></li></ul>";
div.firstChild.innerHTML; //"<li></li>"
Note that .firstChild
will only be ul
if there is no whitespace/text at the beginning of the string.
In that case you could use:
var div = document.createElement("div");
div.innerHTML = " <ul><li></li></ul>";
div.getElementsByTagName("ul")[0].innerHTML //"<li></li>"
instead
var txt="<ul>\n <li>Some text</li>\n <li>Other text</li>\n</ul>";
var result=txt.replace(/^<ul>\s*/,'').replace(/\s*<\/ul>$/,'');
text.replace(/^<ul>/, "").replace(/<\/ul>$/, "");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.