I've searched and searched but can't seem to find the answer I need. I'm still wrapping my head around the whole regex thing don't know enough to solve this problem, which seems far to simple.
I have a string "<a href="#">London<span>35.2miles</span></a>"
I need to chop everything apart from "London"
, but everything I try results in "London<span>35.2miles</span>"
I cannot seem to get rid of the span tags and text, as well as the a tags.
var root = document.createElement("div");
root.innerHTML = '<a href="#">London<span>35.2miles</span></a>';
alert( root.firstChild.firstChild.nodeValue );
The following regular expression should work;
/>([^<]*)/
It matches anything from the first >
until it hits another <
You can combine this with the match()
method, to end up with something like this;
var matches = '<a href="#">London<span>35.2miles</span></a>'.match(/>([^<]*)/);
if (matches) { // "null" if no matches, array if there was.
var london = matches[1];
}
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.