Analysis of regular expressions + range rule verification expressions + js intercepting numbers from strings

Update: Negative numbers and decimal points were not considered when I wrote it before, so if your range rule is: start with "[" or "(" in English state, then an arbitrary value (positive and negative integers, decimals), and then an English comma , and then an arbitrary value (positive and negative integers, decimals), and finally ends with English ")" or "]", similar to: (-10.5,20), you can refer to this regular expression:

let reg = /^(\(|\[)-?[0-9]*.?[0-9]*,-?[0-9]*.?[0-9]*(\)|\])$/; 
let str ='[-12.3,456.7)'; 
console.log(reg.test(str));  // true

————————— Manual segmentation, the following is the original text —————————————————

I often need to use regular expressions in my usual projects, but unfortunately I was too lazy before (of course, the most important thing is that I can’t write too much) and I searched directly on the Internet. The previous ones were also simple. It was nothing more than verifying the format of the mobile phone number, verifying the format of the email address, and occasionally verifying the password. I can't find ready-made ones on the Internet, so I have to try to make a living by myself. . .

However, I still only know the simple ones. The complicated assertions and groups are still in the process of learning, and I hope that some masters will not hesitate to enlighten me.

This requirement is: the administrator defines a range in a work order, such as the score is between 0-100, or the age range of the questionnaire, and then the numbers entered by other users after the work order is transferred are affected by the range defined by the administrator. Therefore, it is required to use regularity to verify whether the range rules entered by the administrator are legal, and then according to the input range, to verify whether the values ​​​​entered by ordinary users are legal.

  1. The first is the core rule verification expression. Our rule is: (or [beginning + number + English comma + number +) or] end, which is this kind: (
    1,100) or (1,100], which is the middle school mathematics range interval. The regular expression I use is:
let reg = /^(\(|\[)-?[0-9]*,-?[0-9]*(\)|\])$/;
//   其中 ^是匹配开头
//  (|\[)是以(或者[开头,在 (和[之前加上\是转义字符,避免表达式把 (和[当成括号的开头来匹配,而是把它们当做字符串 (和[
//  -?是有一个或者0个-,因为可能是负数也可能是正数
// [0-9]*就是重复的数字
// ,就是中间隔开的逗号;
// -?[0-9]* 跟逗号前面的一样,是一个数字校验
// (\)|\]) 是以或]结尾
//  $是匹配结尾

There is a flaw in the above regular expression: the size of the two numbers is not compared, that is, the second number entered by the user may be smaller than the first number. I really can't do anything about it now [manually dumbfounding]

Experience: At first, I felt confused because I didn’t know where the beginning and the end match. Now I don’t really understand it. It feels like ^ matches from the beginning to the end, and $ matches from the end to the beginning. . . Currently this does not affect, what matters is the middle rule

  1. After getting the rules of the string, extract the maximum and minimum values ​​from the string (the difference between () and [] is not considered here, because the interval of () does not include the beginning and the end, and the interval of [] includes the beginning and end), the method I use is:
// 从范围的字符串中获取数字
  const pickNumbersFromString = (v) => {
    if (typeof v === 'string') {
      const regEx = /[^\d|^.|^-]/g;
      const t = v.replace(regEx, ',').split(',');
      const r = [];
      t.forEach((item) => {
        if (item || item === 0) {
          r.push(Number(item));
        }
      });
      return r;
    }
    return [];
  };
  // 这里会返回一个数组,数组中就是范围中的两个数字
  let a = '(2,200)';
console.log(pickNumbersFromString(a));  //  [2, 200]
  1. Limit the input maximum and minimum values ​​in the number input box as required

The following is a little knowledge of regularization that I have learned recently:

 // 正则中常见的限定符:
  a* // 字符a出现0次或多次
  a+ // 字符a出现1次或多次
  a? // 字符a出现0次或1次
  a{3} // 字符a出现3次
  a{3,7} // 字符a出现3-7次
  a{3,} // 字符a出现3次以上
  // 或运算符,这个用的还挺多的

The picture comes from the Internet, invaded and deleted
(The picture comes from the Internet, invaded and deleted)

The biggest enemy on the way to learning is fear. If you are interested, you can search for some regular expression video learning, which is still very helpful. Hope this article helps you!

Guess you like

Origin blog.csdn.net/JaneLittle/article/details/128719560