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.

2
  • 2
    This is one of the famous type questions, unfortunately... stackoverflow.com/questions/1732348/… Commented Oct 18, 2018 at 13:01
  • 5
    You're in JS, why don't you use the DOM to parse HTML instead? Commented Oct 18, 2018 at 13:02

6 Answers 6

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}"

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

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];

Comments

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))

Comments

0
asdf = new RegExp('(sd:.+?[}])+', 'gi');
console.log(str.match(asdf));

Comments

0

more general:

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

REGEX 101

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.