正则表达式只允许整数和十进制[重复]

时间:2022-01-12 16:23:29

Possible Duplicate:
Simple regular expression for a decimal with a precision of 2?

可能重复:一个精度为2的小数的简单正则表达式?

i want to know regular expression that would allow only integers and decimal numbers of any length starting from 0 to infinity in javascript. can any one help me in getting that done

我想知道正则表达式,它只允许在javascript中从0到∞的任何长度的整数和小数。有人能帮我完成吗

2 个解决方案

#1


35  

This code would do :

这段代码可以做到:

var str = "1.2"
var regexp = /^[0-9]+([,.][0-9]+)?$/g;
var result = regexp.test(str);
​alert(result);​

Where:

地点:

  • str is the string you want to test
  • str是要测试的字符串
  • regexp is what you're testing the string with (built like: /pattern/modifiers)
  • regexp是您正在测试的字符串(如:/模式/修饰符)
  • result is a boolean, true if it matches, false otherwise
  • 结果为布尔值,如果匹配为真,则为假

You should check this link about the RegExp object in JavaScript: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

您应该在JavaScript中检查关于RegExp对象的链接:http://www.w3schools.com/jsref/jsref_obj_regexp.asp

About the regexp itself :

关于regexp本身:

  • ^ : start of the string
  • ^:字符串的开始
  • [0-9]+ : at least one digit
  • [0-9]+:至少一位
  • [,.] : dot or comma
  • (,。点或逗号
  • [0-9]+ : same as above
  • [0-9]+:和上面一样
  • (xxxxx)? : the expression inside the parenthesis can be present or not
  • (xxxxx)?:括号内的表达式是否存在
  • $ : end of the expression
  • $:表达结束

You should also check Wikipedia page for regexp if you'd like to learn more, it's rather well done.

如果你想了解更多,你也可以查看*的regexp页面,它做得很好。

#2


12  

Try this

试试这个

^[\d.]+$

^- start of line

^——线的开始

[]- array of selections

[]——的选择

\d- any digit

\ d -任何数字

. - dot character

。-点字符

+ - 1 or many

+ - 1或多个

$ - end of line

$ -线的末端

#1


35  

This code would do :

这段代码可以做到:

var str = "1.2"
var regexp = /^[0-9]+([,.][0-9]+)?$/g;
var result = regexp.test(str);
​alert(result);​

Where:

地点:

  • str is the string you want to test
  • str是要测试的字符串
  • regexp is what you're testing the string with (built like: /pattern/modifiers)
  • regexp是您正在测试的字符串(如:/模式/修饰符)
  • result is a boolean, true if it matches, false otherwise
  • 结果为布尔值,如果匹配为真,则为假

You should check this link about the RegExp object in JavaScript: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

您应该在JavaScript中检查关于RegExp对象的链接:http://www.w3schools.com/jsref/jsref_obj_regexp.asp

About the regexp itself :

关于regexp本身:

  • ^ : start of the string
  • ^:字符串的开始
  • [0-9]+ : at least one digit
  • [0-9]+:至少一位
  • [,.] : dot or comma
  • (,。点或逗号
  • [0-9]+ : same as above
  • [0-9]+:和上面一样
  • (xxxxx)? : the expression inside the parenthesis can be present or not
  • (xxxxx)?:括号内的表达式是否存在
  • $ : end of the expression
  • $:表达结束

You should also check Wikipedia page for regexp if you'd like to learn more, it's rather well done.

如果你想了解更多,你也可以查看*的regexp页面,它做得很好。

#2


12  

Try this

试试这个

^[\d.]+$

^- start of line

^——线的开始

[]- array of selections

[]——的选择

\d- any digit

\ d -任何数字

. - dot character

。-点字符

+ - 1 or many

+ - 1或多个

$ - end of line

$ -线的末端