[英]Regex to extract all digits after last occurrence of a special character
I want to extract all digits after last occurrence of character "-" so for example 311-1974-8 should return me 8 and 311-1974-348 should return 348 我想提取最后一次出现的字符“-”之后的所有数字,例如311-1974-8应该返回我8,而311-1974-348应该返回348。
edit: 编辑:
Added clarification from a comment: 添加了注释的说明:
actually it's an external tool which provides it's own inbuild functionalists and i have no other option but to use regex to extract this. 实际上,它是一个外部工具,提供了自己的内置函数学家,我别无选择,只能使用正则表达式来提取它。 No JS can be applied :( 不能使用JS :(
This captures the last number. 这将捕获最后一个数字。
var str = '311-1974-348';
var matches = str.match(/-(\d+)$/);
var match = matches ? matches[1] : null;
console.log("matched? " + match);
Try matching on /[^-]+$/
, eg: 尝试在/[^-]+$/
上进行匹配,例如:
var s = '311-1974-348';
s.match(/[^-]+$/); // => ["348"]
为什么不简单地分裂呢?
var str = input.split('-').pop();
You mentioned in a comment it's for an external tool so... 您在评论中提到这是针对外部工具的,因此...
-([0-9]+)$
dunno how your tool handles captured groups or anything... 不知道您的工具如何处理捕获的组或其他任何东西...
Try this /[0-9]+$/
. 试试这个/[0-9]+$/
。 It worked on both the inputs you provided. 它适用于您提供的两种输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.