admin管理员组

文章数量:1418400

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.

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.

Share Improve this question edited Jan 27, 2014 at 1:43 Palec 13.6k8 gold badges76 silver badges143 bronze badges asked Jan 27, 2014 at 1:17 craCHcraCH 1131 silver badge10 bronze badges 1
  • What language are you using? The regex is mostly language-independent (something like -[^-]*$), but the mand to execute the replacement is language-dependent. – elixenide Commented Jan 27, 2014 at 1:23
Add a ment  | 

1 Answer 1

Reset to default 8

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.

本文标签: Remove word after last dash with regex in JavaScriptStack Overflow