Hello,
I have a string
<p attribute="testAttribute">Some text</p>
I would like to “extract” the attribute value of the paragraph “tag” for further processing. But I don’t know if it’s possible in Javascript to get the value since the content is plain string.
Anyone know how to do this?
Thanks!
- Bought between 10 and 49 items
- Contributed a Blog Post
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 4-5 years
- Item was Featured
- Microlancer Beta Tester
- Referred between 100 and 199 users
- Sold between 10 000 and 50 000 dollars
If you use jQuery
var str = '<p attribute="testAttribute">Some text</p>';
var my_obj = $(str);
console.info(my_obj.attr('attribute')); // => testAttribute
But don’t use jQuery just to extract the attribute, an alternative is to do it manually (insert the string to the DOM ), and use an identifier for it, but I’m lazy to do that 
- Microlancer Beta Tester
- Author had a Free File of the Month
- Has been a member for 3-4 years
- Item was Featured
- Author was Featured
- Austria
- Exclusive Author
- Referred between 200 and 499 users
Or with regex:
var str = '<p attribute="testAttribute">Some text</p>'; alert(str.match(/<p attribute="(\w+)">(.*)<\/p>/)[1]);</p>
I’m no expert in regular expressions, I find it hard to manage it :/. I’ll try the way that omarabid just proposed.
Thanks!
