最后一个冒号后匹配字符串 [英] match string after the last colon

查看:123
本文介绍了最后一个冒号后匹配字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串


foo:21,bar:11

foo: 21, bar: 11

其中foo和bar不是常量,所以我试图匹配最后(冒号)字符后的所有数字。

where foo and bar are not constants, so I'm trying to match all the digits after the last ":"(colon) character.

const myString = 'foo: 21, bar: 11'
myString.match(/\d+/g).shift().join() -> 11

我可以用纯正则表达式做同样的事吗?

can i do the same just with pure regex?

谢谢!

推荐答案

使用否定正则表达式可以使用此正则表达式:

Using negative regex you can use this regex:

/\d+(?![^:]*:)/g

RegEx演示

(?![^:] *:)是负面预测断言没有在我们匹配的数字之前。

(?![^:]*:) is negative lookahead to assert that there is no : ahead of digits we are matching.

代码演示:

var myString = 'foo: 21, bar: 11';
console.log(myString.match(/\d+(?![^:]*:)/g));

这篇关于最后一个冒号后匹配字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆