715 posts
  • Author Level 2
  • Collector Level 2
  • Exclusive Author
  • Belgium
+1 more
Hy-Studio
says

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!

517 posts Abid Omar
  • 7 Years of Membership
  • Affiliate Level 4
  • Author Level 7
  • Beta Tester
+9 more
omarabid
says

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 :p

2280 posts
  • Top Monthly Author
  • Weekly Top Seller
  • Community Superstar
  • Industrious API Inventor
+15 more
revaxarts
says

Or with regex:

var str = '<p attribute="testAttribute">Some text</p>';
alert(str.match(/<p attribute="(\w+)">(.*)<\/p>/)[1]);</p>
715 posts
  • Author Level 2
  • Collector Level 2
  • Exclusive Author
  • Belgium
+1 more
Hy-Studio
says

I’m no expert in regular expressions, I find it hard to manage it :/. I’ll try the way that omarabid just proposed.

Thanks!

by
by
by
by
by
by