JS to find the last character of the string and remove it

The first uses the replace method of the string

var str = "hello-world-"
        // \S non-blank character $ ending {1} Match a
        str = str.replace(/\S{1}$/, '');

The second way is to use string interception and there are three APIs for string interception and I’m only going to talk about two of them

var str = "hello-world-"
 console.log(str.slice(0, -1));

console.log(str.substring(0, str.length - 1));

The final output is hello-world
Watch me keep my front-end knowledge updated

Read More: