sercan sercan - 1 year ago 69
AngularJS Question

How to put prefix on expressions surrounded by curly brackets

What could be the javascript regex expression of putting prefix to all values in angular curly braclets ?

//prefix : listItem
<span>{{name}} {{surname}}</span>
<span>{{}} {{listItem.surname}}</span>

Answer Source

You can use String.prototype.replace() with RegExp /(\w+)(?=\}\})/g to match word followed by }}

var span = document.querySelector("span");
span.textContent = span.textContent.replace(/(\w+)(?=\}\})/g, function(match) {
  return "listItem." + match
<span>{{name}} {{surname}}</span>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download