Categories
JavaScript Answers

How to remove everything after last backslash with JavaScript?

Spread the love

To remove everything after last backslash with JavaScript, we call the substr and lastIndexOf methods.

For instance, we write

const t = "\\some\\route\\here";
const newT = t.substr(0, t.lastIndexOf("\\"));

to call lastIndexOf to return the last index of '\\'.

Then we call substr to return the substring in t before index 0 and the index returned by lastIndexOf.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.