Javascript 匹配字符串末尾的正则表达式模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6494915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:01:56  来源:igfitidea点击:

regex pattern to match the end of a string

javascriptregex

提问by user815460

Can someone tell me the regex pattern to match everything to the right of the last "/" in a string.

有人可以告诉我匹配字符串中最后一个“/”右侧的所有内容的正则表达式模式吗?

For example, str="red/white/blue";

例如, str="红/白/蓝";

I'd like to match "blue" because it is everythingto the right of the last "/".

我想匹配“蓝色”,因为它是最后一个“/”右侧的所有内容

Many thanks!

非常感谢!

采纳答案by mrk

Use the $metacharacterto match the end of a string.

使用$元字符匹配字符串的结尾。

In Perl, this looks like:

在 Perl 中,这看起来像:

my $str = 'red/white/blue';
my($last_match) = $str =~ m/.*\/(.*)$/;

Written in JavaScript, this looks like:

用 JavaScript 编写,这看起来像:

var str = 'red/white/blue'.match(/.*\/(.*)$/);

回答by Kirill Polishchuk

Use this Regex pattern: /([^/]*)$

使用这个正则表达式模式: /([^/]*)$

回答by KingCrunch

Should be

应该

~/([^/]*)$~

Means: Match a /and then everything, that is not a /([^/]*) until the end ($, "end"-anchor).

意思是:匹配 a/然后匹配所有内容,直到最后(,“end”-anchor)才不是/( ) 。[^/]*$

I use the ~as delimiter, because now I don't need to escape the forward-slash /.

我使用~as 分隔符,因为现在我不需要转义正斜杠/

回答by NickAldwin

Something like this should work: /([^/]*)$

这样的事情应该工作: /([^/]*)$

What language are you using? End-of-string regex signifiers can vary in different languages.

你使用什么语言?字符串结尾的正则表达式符号在不同的语言中可能会有所不同。

回答by Phillip Kovalev

Use following pattern:

使用以下模式:

/([^/]+)$