ragavi gr
ragavi gr

Reputation: 33

How to remove HTML tags in a string Handlerbar.js Using regex

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

Answers (2)

Mesut Can
Mesut Can

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

wazz
wazz

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

Related Questions