craCH
craCH

Reputation: 103

Remove word after last dash with regex in JavaScript

I’ve defined string word-word-word-test and I want to remove the -test (all behind the last dash). How can I do that with a RegEx?

My /-.*$/ works only if there are no other dashes in the string.

Upvotes: 2

Views: 2616

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

You can use -[^-]*$ to replace "dash followed by zero or more non-dash characters anchored to the end of the string." You may also just want to use (-test)*$ to replace all -test instances at the end of the string.

Upvotes: 8

Related Questions