繁体   English   中英

如何使用正则表达式获取 Aadhar Number 的最后一个字符

[英]How can I get last characters of Aadhar Number using Regex

我有一个 Aadhar 号码。

var temb ="xxxxxxxx5267"

我需要得到最后 4 位数字(5267)

我试过(.{3})\\s*$它不起作用。

您可以使用带负号的 slice 方法(通过反向匹配字符):

 var temb ="xxxxxxxx5267"; console.log(temb.slice(-4))

用这个

[0-9]{4}$

在此处查看演示: https : //regex101.com/r/etasd5/1/

正则表达式运行良好。 您是否使用了正确的正则表达式组捕获方法?

 const temb ="xxxxxxxx5267" const res = temb.match(/(.{4})\\s*$/g) console.log(res[0])

你可以在这里做一个正则表达式替换:

 const temb ="xxxxxxxx5267" const num = temb.replace(/^.*(\\d{4})$/, "$1"); console.log(num);

另一种变体:

 const temb ="xxxxxxxx5267" const num = temb.replace(/^.*(?=\\d{4}$)/, ""); console.log(num);

暂无
暂无

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

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