开发者

Match HTML tag's content with a Javascript RegEx

I have the following HTML as a string in my JavaScript function:

<p>one</p> <p align='center'>two</p>

I want to extract this string:

"onetwo" (without quote开发者_如何学运维s obviously)

Can you please suggest some pure JavaScript code (jQuery is also OK...) to get tags' content?


Using jQuery you don't need a complex regex, you can easily parse the HTML and use the DOM:

var s = "<p>one</p> <p align='center'>two</p>";
var wrapper = $('<div />').html(s);
var text = wrapper.text();

In this case $(s).text() would have also worked, but it will fail if you have free text on the first level (e.g. <p>1</p>2), so I usually avoid it.

Note that the result here is "one two" (not "onetwo"), because you have a space between the <p> tags.
If that's a problem, you can use wrapper.children().text() or wrapper.find('p').text(), for example, according to your exact needs.

Working example: http://jsbin.com/osidi3


I made the following Regex to grab content from XML tags. This will only work with a tag that has content and is followed by a closing tag. Will not get contents of tags that contain other tags. The tag name is in capture group 1 and the tag content is in capture group 2. This will work to get all content including <, >, ", ' and & inside of tag content.

<([^\s>]+)\s?[^>]*>(.*)(?:<\/\1)>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜