JS regular expression is limited to 1-2 integers, or at most contains two decimal places

Time:2021-3-5

Test code

<script type="text/javascript"> 
//1. You can only enter numbers or decimal points, only integers, integers plus decimals
var reg1=/(^[0-9]{1,2}$)|(^[0-9]{1,2}[\.]{1}[0-9]{1,2}$)/;
console.log (reg1. Test ("+" empty string false ");
console.log(reg1.test("1")+" 1 true");
console.log(reg1.test("10")+" 10 true");
console.log(reg1.test("10.")+" 10. false");
console.log(reg1.test("100")+" 100 false");
console.log(reg1.test("100.1")+" 100.1 false");
console.log(reg1.test("10.1")+" 10.1 ture");
console.log(reg1.test("10.10")+" 10.10 true");
console.log(reg1.test("10.101")+" 10.101 false");
console.log(reg1.test("0.101")+" 0,101 false");
console.log(reg1.test("110.101")+" 110.101 false");
console.log(reg1.test("a")+" a false");
console.log(reg1.test("*")+" * false");
console.log(reg1.test("..")+" .. false");
</script>

design sketch

If you do not limit two integers, such as at least one integer, [0-9] {1,} is enough

Regular to or “|” in JS

//It must start with a number and end with a number. It can contain a comma in the middle. If there is only one number, it can only be a number
  var regx1=/(^[0-9]{1,}[0-9,]{0,}[0-9]{1,}$)|(^[0-9]{1}$)/;

The regular expression table matches all two digits, and the ten digits are 1

For example, I want to match all the shapes like param_ Tag = 12, such an equal sign is followed by two digits, and the ten digits are 1. The following regular expressions can be used for matching:

parma_tag=\<1[0-9]\>

Regular expression: matches two digits, and the first digit cannot be 0

^[1-9][0-9]$

JS regular, can only input numbers and at least two significant digits, the number of digits can be up to five (the limited digits can be adjusted)

var reg=/^[1-9]\d{0,3}\.\d$|/^[1-9]\d{0,2}\.\d{2}$|^[1-9]\d{1,4}$|^[0]\.\d{2,4}$/;

Code analysis:

Var reg = / ^ [1-9] - D {0,3} \. [D $/; // first (1-9), middle zero to three digits, then dot, one decimal (0-9) after dot
Var reg = / ^ [1-9] \ \ D {0,2} \. [D {2} $/; // first (1-9), middle zero to two digits, then dot, two decimal places after dot (0-9)
Var reg = / ^ [1-9] [D {1,4} $/; // first (1-9), one to four digits from middle to end, no decimal
Var reg = / ^ [0] \. [D {2,4} $/; // first (0), followed by a dot, followed by two to four decimal places (0-9)
//If they are written together, they are connected by the symbol |, which means or. If any condition is satisfied, they are qualified

JS regular expression — limit number length

For example: limit the number of words to 4

var reg = /^\d{4}$/

Explanation: it starts with and ends with four numbers, so the number length is limited to four.

Note: also based on these four numbers

So far, this article about JS regular expression is limited to 1-2 integers, or at most contains two decimal places. For more related JS 2-digit integer regular content, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!