最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove word after last dash with regex in JavaScript - Stack Overflow

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论