sercan sercan - 1 year ago 48
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
//before
<span>{{name}} {{surname}}</span>
//after
<span>{{listItem.name}} {{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>