How to split and slice a string in Handlebars?

To split and slice a string in Handlebars; you need to register a custom helper for Handlebars. There is an example about how to register a custom helper for Handlebars in official Handlebars website https://handlebarsjs.com/#helpers

This is my custom helper

Handlebars.registerHelper('splitUrl', function(url) {return url.split("/").slice(-1,str.length);});

Which splits the url string

var url = "https://twitter.com/akarshseggemu";

and slices it to return only the last part i.e. my username “akarshseggemu”.

You can use the custom helper in the hbs file like this

<p>{{splitUrl url}}</p>

Junior Software Engineer | M.Sc. Computer Science from Technische Universitaet Berlin

Junior Software Engineer | M.Sc. Computer Science from Technische Universitaet Berlin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store