Z
Z
zeni1agent2021-07-01 11:50:16
JavaScript
zeni1agent, 2021-07-01 11:50:16

How to remove the last word in a line?

I have a string I want to get from it I tried to do so.
var names = 'red/blue\green\yellow/brown'
var names = 'red/blue/green/yellow/'

var names =  'red/blue\green\yellow/brown'
var nameList = names.split('/');
console.log(nameList )
nameList.pop()
console.log(nameList )
var names2 = nameList.join('/') 
console.log(names2 )

But slashes broke everything.
I tried like this
var str =  'red/blue\green\yellow/brown'
var lastIndex = str.lastIndexOf("/");
str = str.substring(0, lastIndex);
console.log(str)

but the result is the same
slashes can be rotated in different directions.
How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
v3shin, 2021-07-01
@v3shin

let str = 'red/blue\green\yellow/brown';
console.log(str.replace(/\W\w+$/, ''));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question