正则表达式以匹配数字,然后是下划线和数字? [英] Regular expression to match numbers followed by underscore and numbers?

查看:87
本文介绍了正则表达式以匹配数字,然后是下划线和数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写正则表达式以使用javascript进行验证.我的要求是验证数字,然后跟下划线,并且它也应该有数字.

I am trying to write a regular expression to do validation in javascript. My requirement is to validate numbers followed by underscore and again it should have numbers.

For example: 123456789_123456789

长度不是约束.它可以具有 n 个数字,下划线和 n 个数字.目前,我尝试使用此 [0-9] _ [0-9] .有什么更好的方法吗?任何建议表示赞赏.

Length is not a constraint. It can have n numbers, underscore and n numbers. Currently i tried with this [0-9]_[0-9]. Is there any better way of doing it ? Any suggestion is appreciated.

谢谢,Sreekanth

Thanks, Sreekanth

推荐答案

您几乎明白了.正确的正则表达式为:

You almost got it. The correct regex would be :

^[0-9]{1,}_[0-9]{1,}$

^[0-9]+_[0-9]+$

正则表达式表示:"一个或多个数字( [0-9] {1,} ),后跟下划线( _ ),然后再输入一位或多位数字( [0-9] {1,} ).

The regex means: "one or more digits ([0-9]{1,}), followed by an underscore (_) and then again one or more digits ([0-9]{1,}).

此匹配项:

12312_123123
1_1

但不匹配:

123123_
_123123
_
123123_1231ddd
123dd_123
dd123_123

这篇关于正则表达式以匹配数字,然后是下划线和数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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