0

Can you help how to parse the regex i have tried a lot but not able to do.

Input : <a onclick="javascript:collapse('displayText','hideText','whyInfo-arrow-image')"><span sd:text="${whyInfoLabel}">Learn more about Authentication</span> <img id="whyInfo-arrow-image" sd:src="${downwardArrowImage}" height="20" width="20" >

Expected : sd:text="${whyInfoLabel}" and sd:src="${downwardArrowImage}"

My attempt 1 : "/\$\{(\w*)\}/g" it can pick only {whyInfoLabel} & {downwardArrowImage}

My attempt 2 : "/sd.*\$\{(\w*)\}/g" this is not able to split.

I am very new to Regex and to JavaScript.

Any help will be great.

maxime1992
  • 22,502
  • 10
  • 80
  • 121
Akshay
  • 177
  • 1
  • 3
  • 14
  • 2
    This is one of the famous type questions, unfortunately... https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Katana314 Oct 18 '18 at 13:01
  • 5
    You're in JS, why don't you use the DOM to parse HTML instead? –  Oct 18 '18 at 13:02

6 Answers6

1

You're in JS, why don't you use the DOM to parse HTML instead? Regex isn't designed to parse HTML, but the DOM provides a utility that is designed specifically for that purpose.

If your browser supports it, you should consider doing this using the DOMParser instead:

var html = "<a onclick=\"javascript:collapse('displayText','hideText','whyInfo-arrow-image')\"><span sd:text=\"${whyInfoLabel}\">Learn more about Authentication</span>  <img id=\"whyInfo-arrow-image\" sd:src=\"${downwardArrowImage}\" height=\"20\" width=\"20\" >";

var parser = new DOMParser();
var doc = parser.parseFromString(html, "text/html");

var attrText = doc.querySelector('span').getAttribute("sd:text");
console.log(attrText);

var attrSrc = doc.querySelector('img').getAttribute("sd:src");
console.log(attrSrc);

"${whyInfoLabel}"
"${downwardArrowImage}"

0

Try this: /(sd:.*=".*")/Ug

Or this: /\{(.*)\}/Ug

rib
  • 94
  • 6
0

This should give you both results in an array:

/sd:(?:text|src)=\"[^\"]*"/g

The regex matches 'sd:' at start, followed by 'text' or 'src', a equal sign, a double quote followed by zero or more characters not being a double quote, and finally a double quote.

Just call (assuming your text in a variable called 'text':

var regex =/sd:(?:text|src)=\"[^\"]*"/g;
var matches = text.match(regex);
var match0 = matches[0];
var match1 = matches[1];
Poul Bak
  • 10,450
  • 5
  • 32
  • 57
0

You can try this regex. Hope this helps.

const regex = /\w+:\w+="\$\{\w*\}"/g;

const input = '<a onclick="javascript:collapse(' + 'displayText' + ',' + 'hideText' + ','+ 'whyInfo-arrow-image' + ')"><span sd:text="${whyInfoLabel}">Learn more about Authentication</span>  <img id="whyInfo-arrow-image" sd:src="${downwardArrowImage}" height="20" width="20" >';

console.log(input.match(regex))
Alex G
  • 1,897
  • 2
  • 10
  • 15
0
asdf = new RegExp('(sd:.+?[}])+', 'gi');
console.log(str.match(asdf));
Sven Liivak
  • 1,323
  • 9
  • 10
0

more general:

(?<=["\s])[^\s]+?\s?=\s?"\$\{(?:\w*)\}"

REGEX 101

Andre Elrico
  • 10,956
  • 6
  • 50
  • 69