tvance tvance - 5 months ago 16
HTML Question

Javascript RegExp Selector for HTML Template Strings

I need some help with my JavaScript regex. I need a way to select the following:

${user.name}
${user.age}


from the following html:

<tr>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>


There could be multiple
<td>
with different values in them and the 'user' part of the string is not always going to be user. It could be anything.

Ideally I would like them back as an array, although at the moment I'll settle for anything.

Answer

I am not regex expert but I hope it will work.

var str = "your html"
var matches = str.match(/\${[a-zA-Z0-9.]+}/g);

For your given input html, output will be

["${user.name}", "${user.age}"]
Comments