我有一个 Aadhar 号码。var temb ="xxxxxxxx5267"我需要得到最后 4 位数字(5267)我试过(.{3})\s*$它不起作用。
4 回答

当年话下
TA贡献1890条经验 获得超9个赞
你可以在这里做一个正则表达式替换:
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);

白板的微信
TA贡献1883条经验 获得超3个赞
正则表达式运行良好。您是否使用了正确的正则表达式组捕获方法?
const temb ="xxxxxxxx5267"
const res = temb.match(/(.{4})\s*$/g)
console.log(res[0])

一只斗牛犬
TA贡献1784条经验 获得超2个赞
您可以使用带负号的 slice 方法(通过反向匹配字符):
var temb ="xxxxxxxx5267";
console.log(temb.slice(-4))
添加回答
举报
0/150
提交
取消