I have some HTML code as a string. For example,
<button data-a=12 data-b=444 data-c='xyz'>Reply</button>
This string is stored in a variable in Javascript. There can be any number of data attributes in the string. The value of each attribute can be anything. I have to extract the value of a particular attribute, say data-b
, for this case.
Apart from the obvious string manipulations, is there a more concise way to somehow de-stringify this "stringy" HTML code and extract the data-attribute value using jQuery? Is there another better way to do this? Which one is the best with regards to efficiency and readability?
Edit: The values of some of these attributes (other than the one I want to extract) come from user input. I use a sanitizer library before rendering those inputs as data attribute values, so the string should be XSS safe, but it can look very weird. For example, this string could be,
<button data-a=10 data-b=76 data-c='>1' alert(502); data-d=34>Reply</button>