2

I'm looking for the Javascript to parse the following HTML:

<p>random text random text random text random text</p>
<kbd><h2>Heading One</h2>Body text Body text Body text Body text</kbd>
<p>random text random text random text random text</p>

... and return just:

Heading One

In other words, I'd like to strip all tags and Body Text from within the <kbd> tags.

Any ideas would be greatly appreciated!

2
  • do you want a pure javascript solution or a jQuery (or other similar library) whould be enought? Commented Oct 23, 2011 at 21:38
  • 2
    Your desired output does not match what you say you want to do. Commented Oct 23, 2011 at 21:39

3 Answers 3

2
var input = /* that HTML string here */;
var div = document.createElement('div');

div.innerHTML = input;

var h2 = div.getElementsByTagName('h2')[0];
var text = h2.innerText || h2.textContent;

alert(text); // alerts "Heading One"

Reference:

Demo:

Sign up to request clarification or add additional context in comments.

Comments

2

Regex?

var s = "<p>random text</p>\n" +
  "<kbd><h2>Heading One</h2>Body text</kbd>\n" +
  "<p>random text</p>";

s.match(/<h2>(.*?)<\/h2>/)[1] // == "Heading One"

This matches group one as the shortest possible (.*?) string between <h2>...</h2>.

You can find all matches using the g option.

s.match(/<h2>(.*?)<\/h2>/g) // == ["<h2>Heading One</h2>"]

Note that groups are not accessible.

For multiline content between tags, use

s.match(/<tag>[\s\S]*?<\/tag>/ig)

Comments

0

if you include jquery (jquery.com) you can do this:

var heading=$("h2").html();

1 Comment

That only works if the HTML to be parsed is somewhere in the page already.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.