Reputation: 33
I have a string like
var string =`<div><b>Test data</b></div><div><div>Invalid DateTime Only YYYY/MM/DD HH:mm:ss is allowed</div></div><br><div><div>Invalid DateTime</div>`
Remove those HTML tags from the string using regex in Handlebar. My code looks like
This DISPLAYVALUE is my exact String, I have to replace the HTML tags here
<span title="{{{this.DISPLAYVALUE}}}"> {{strip-scripts this.DISPLAYVALUE}}
<a href="javascript:void(0);" class="remove-ico right" data-filter-val="{{{this.DISPLAYVALUE}}}" datacolname='{{@root.currentColName}}'>
<i class="fa fa-times-circle"></i>
</a>
</span>
Excepted Result:- Test data Invalid DateTime Only YYYY/MM/DD HH:mm:ss is allowed Invalid DateTime
Upvotes: 1
Views: 1488
Reputation: 291
There are HELPER methods that you can use directly in handlebars templates.For example you can create a seperate handlebars helper js file like below;
(function() {
Handlebars.registerHelper('stripScripts', function(param) {
var regex = /(<([^>]+)>)/ig
return param.replace(regex, "");
});
})();
Then you can simply call this method in your template like;
<span title="{{{this.DISPLAYVALUE}}}"> {{stripScripts this.DISPLAYVALUE}}
<a href="javascript:void(0);" class="remove-ico right" data-filter-val="{{{this.DISPLAYVALUE}}}" datacolname='{{@root.currentColName}}'>
<i class="fa fa-times-circle"></i>
</a>
</span>
Upvotes: 1
Reputation: 5078
If you can use JS:
function myFunction() {
var str = "<div><b>Test data</b></div><div><div>Invalid DateTime Only YYYY/MM/DD HH:mm:ss is allowed</div></div><br><div><div>Invalid DateTime</div>";
var res = str.replace(/<div>/g, "");
var res = res.replace(/<\/div>/g, "");
var res = res.replace(/<b>/g, "");
var res = res.replace(/<\/b>/g, "");
var res = res.replace(/<br>/g, " ");
console.log(res);
}
Upvotes: 0