简体   繁体   English

如何获得特殊字符后的最后一个字?

[英]How can I get the last word after a special character?

So far I have the following: 到目前为止,我有以下内容:

 \- (.*)

And from the following text: 并且来自以下文本:

September 2014 - Media

I need to get "Media", tho I am getting "- Media" 我需要获取“媒体”,如果我正在获取“-媒体”

Your regex is correct just print the group index 1 instead of group 0 and you don't need to escape - symbol. 您的正则表达式是正确的,只需打印组索引1而不是组0,就不需要转义-符号。

DEMO DEMO

See the captured string Media at the right side in the above demo link. 请参阅上面的演示链接中右侧捕获的字符串Media

OR 要么

Use the below regex to match only Media , 使用以下正则表达式仅匹配Media

- \K.*

DEMO DEMO

You could use a lookbehind if your language fail to support \\K , 如果您的语言不支持\\K ,则可以使用回溯方式

(?<=- ).*

DEMO DEMO

> "September 2014 - Media".match(/[^- ]+$/g)
[ 'Media' ]
> var r = "September 2014 - Media";
undefined
> console.log(/- (.*)/.exec(r)[1])
Media

If your language allows lookbehind, try : 如果您的语言允许您回头看,请尝试:

  (?<=-)(.*) 

if doesn't simply use : 如果不只是使用:

  ([^-]*)$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM