简体   繁体   English

正则表达式与最后一场比赛

[英]Regex with last match only

I am trying write a regex to get last digit. 我正在尝试编写正则表达式以获取最后一位数字。

My string: name[0][0]. 我的字符串:name [0] [0]。

My regex: str.match(/d+/g) 我的正则表达式:str.match(/ d + / g)

It return all match. 它返回所有匹配项。 Can you help me make regex return only last match? 您能帮我让正则表达式只返回最后一个匹配项吗?

To get the last digit, 要获得最后一位数字,

\d(?=\D*$)

To get the last number. 获取最后的号码。

\d+(?=\D*$)

DEMO 演示

\\d+ matches one or more digits. \\d+匹配一个或多个数字。 + repeats the previous token or more times. +重复上一个令牌或多次。 (?=\\D*$) called positive lookahead assertion which asserts that the match would be followed by any number of non-digit characters further followed by end of the line. (?=\\D*$)称为正向超前断言,它断言匹配后将跟任意数量的非数字字符,并在该行的末尾。

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

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