Home  >  Q&A  >  body text

javascript - Find a js regular rule: only numbers and English periods can be entered, not two consecutive dots, and cannot start or end with "."

Find a regular rule to use for ip fuzzy search in the text boxUse

The correct approximate format is:
1.1
1.11.1
1.1.11

Satisfy the following filter items:
Cannot start with ".": .1.1.11
Cannot end with ".": 1.1.11.
Cannot have 2 consecutive ".": 1. .1.11
cannot contain symbols other than the English period ".": 1@1.1
cannot contain spaces: 1 .1 .1
cannot contain letters or Chinese characters
Only 1 number can pass

Because I am not very good at regular expressions, I use a stupid method to eliminate them step by step. My idea is to first remove the inner and outer spaces:
1. Remove the inner spaces: /^S*$/
2. Remove spaces on both sides:

    while((value.indexOf(" ") == 0) && (value.length > 1)) {
        return false;
    }
    while((value.lastIndexOf(" ") == value.length - 1) && (value.length > 1)) {
        return false;
    }

3. Then exclude symbols: /<|>|||*|?|\|"|/|&|#|@|!|~|(|)/;
4. Remove Chinese The regular rules: /^[u4e00-u9fa5] $/;
It won’t be used later, and I feel that these methods are very cumbersome and redundant. Is there any regular rules that can solve this problem? Thank you!

天蓬老师天蓬老师2686 days ago1060

reply all(5)I'll reply

  • 学习ing

    学习ing2017-06-12 09:24:18

    /^(((\d+[.])+\d+)|\d)$/

    reply
    0
  • 代言

    代言2017-06-12 09:24:18

    /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/
    

    Among them, (d|[1-9]d|1dd|2[0-4]d|25[0-5]) is specially used to match numbers from 0 to 255. The above regular expression can Understood as: ([0~255].){3}[0~255]

    reply
    0
  • 迷茫

    迷茫2017-06-12 09:24:18

    To put it bluntly, it means starting with a number and ending with a number, and you can insert a period in the middle of the number.

    /^(\d+[.])+\d+$/
    

    The IP address usually has two dots, so

     /^(\d+[.]){2}\d+$/

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-12 09:24:18

    The IP address is in the format of x.x.x.x. The longest x is a 3-digit integer and does not exceed 255, so my regular expression is as follows:

    /^([0-9]|[1-9]d|1dd|2[0-4]d|25[0-5])(.([0-9]|[1-9]d| 1dd|2[0-4]d|25[0-5])){0,3}$/

    This part is mainly to match numbers between [0,255]

    ([0-9]|[1-9]d|1dd|2[0-4]d|25[0-5])

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-12 09:24:18

    /^d+(?:.d+)*$/
    Has been personally tested to meet the subject’s needs

    reply
    0
  • Cancelreply