0

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.

2
  • Why regex? Isn't your a tag a dom element? Commented Jul 18, 2012 at 10:50
  • 2
    Cthulhu, my lord, he comes whilst virgin ponies wheep the blood of the unborn... or something like that Commented Jul 18, 2012 at 10:56

2 Answers 2

3
var root = document.createElement("div");
root.innerHTML = '<a href="#">London<span>35.2miles</span></a>';
alert( root.firstChild.firstChild.nodeValue );
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for handling it as DOM element :) Jus added a comment for the same in OP
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.