0

Given the following string, how would I extract the string between the quotes that follows data-selected (i.e. THIS-DATA). I would like it to work regardless of whether single or double quotes are used, or whether there is any white-space before or after the equal sign. Thank you

<td><img alt="abc" src="lib/templates/administrator/images/star.png" data-selected="THIS_DATA" title="None" class="vtip"></td>
user1032531
  • 24,767
  • 68
  • 217
  • 387

3 Answers3

1

Can you use jQuery? Not sure why you need to use a regex.

var $el = $(".vtip");
var data = $el.attr('data-selected');

or native javascript:

var el = document.querySelector(".vtip");
var data = el.getAttribute('data-selected');

or native javascript with dataset:

var el = document.querySelector(".vtip");
var data = el.dataset.selected;

More info using dataset: https://developer.mozilla.org/en-US/docs/DOM/element.dataset

chovy
  • 72,281
  • 52
  • 227
  • 295
  • Thanks Chovy. Creative solution! Unfortunately, I am unable to use jQuery or similar approach, but just needed a regex. If the desire for single quotes or whitespace prevents this, I can eliminate these requirements. – user1032531 Nov 24 '12 at 20:01
  • I am creating a jQuery plugin. When configuring it, user can specify parsing function on certain items. I am looking for a generic approach to parse the data. – user1032531 Nov 25 '12 at 14:10
1

Simple regex, assuming THIS_DATA doesn't contain quotes:

var reg = /data-selected\s*=\s*["']([^"']*)["']/;

To use:

function extractData(yourString) {
    var match = str.match(yourString);
    return match ? match[1] : "";
}

Edit:

Or even simpler:

var data = str.replace(/.*data-selected\s*=\s*["']([^"']*)["'].*/, "$1");
beatgammit
  • 19,817
  • 19
  • 86
  • 129
  • Thanks tjameson. Thanks, let me test it out. I really need to better learn regex! – user1032531 Nov 24 '12 at 20:12
  • `$1` is the first parenthesized part of the regex (the part we're after). I just made sure the regex matched the entire line (note the `.*` at the beginning and end) so the replace would just extract the part you want. `["']` matches a single or double quote, `[^"']` matches everything else. Happy regexing! – beatgammit Nov 24 '12 at 20:25
  • Thanks again. I am sure this will work and will select it as the answer, but it will probably first take me an hour to get it working! – user1032531 Nov 24 '12 at 20:28
  • Use the jQuery example then. Still don't understand why you would ever use regex to parse html, when there are already both jQuery and native solutions that are bulletproof. – chovy Nov 25 '12 at 17:59
0

You may parse string as a dom

var xmlStr = '<td><img alt="abc" src="lib/templates/administrator/images/star.png" data-selected="THIS_DATA" title="None" class="vtip"></td>';
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlStr, "text/xml");

var path = 'img/@data-selected';

var nodes = xmlDoc.evaluate(path, xmlDoc, null, XPathResult.ANY_TYPE, null);


var result = nodes.iterateNext();
var data-selected  = result.textContent;
dmi3y
  • 3,482
  • 2
  • 21
  • 32