can someone help me a code a regex which will grap the contents of this p :
<p class="bc_shirt_name">101</p>
Thanks
It is preferable to use a DOMParser when your markup is well formed.
var parser = new DOMParser();
var doc = parser.parseFromString('<p class="bc_shirt_name">101</p>', "application/xml");
var contents = doc.documentElement.textContent;
This works for pretty much any XML, not just XHTML. I could use this on <tag>foo</tag>
as well.
Here is a demonstration: http://jsfiddle.net/vEeXX/
Your browser should be able to parse HTML, no?
var div = document.createElement("div");
div.innerHTML = '<p class="bc_shirt_name">101</p>';
var p = div.getElementsByTagName("p")[0];
var html = p.innerHTML; // content as html
var text = p.textContent || p.innerText; // content as plain text
There are many ways but here is a way to use regex as asked by the original question:
var html = "<stuff><p class=\"bc_shirt_name\">101</p></stuff>";
var matches = html.match(/<p\s+class="bc_shirt_name">[\S\s]*?<\/p>/gi);
matches = matches[0].replace(/(<\/?[^>]+>)/gi, ''); // Strip HTML tags?
alert(matches);
JSFiddle Demo: http://jsfiddle.net/tGTb7/
var str = "<p class="bc_shirt_name">101</p>";
var match, result = "",
regex = /<p\s+class="bc_shirt_name">(.*?)<\/p>/ig;
while (match = regex.exec(str)) {
result += match[1];
}
alert(result);
The result variable will contain the required data. hope this helps.
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.