Regular Expressions in JS <<<

About regular expressions:  

  A regular expression is a logical formula that operates on strings (including ordinary characters (for example, letters between a and z) and special characters (called "metacharacters")), that is, using some pre-defined specific characters , and the combination of these specific characters to form a "rule string", this "rule string" is used to express a filtering logic for strings. A regular expression is a text pattern that describes one or more strings to match when searching for text.

First, the declaration of regular expressions

 1. Literal declaration:

   /regex rules/match patterns

   例:var reg = /^abc$/i;

 2. New keyword declaration:

   var reg = new RegExp("^abc$",i);

Second, the description of regular expression rules

  Rule description:

 symbol describe 
/.../  Represents the beginning and end of a pattern 
 ^ matches the beginning of the string 
 $ Matches the end of the string 
 \s any whitespace characters 
\S any non-whitespace character
 \d Match a digit character, equivalent to [0-9] 
\D  Any character except numbers, equivalent to [^0-9] 
\w Matches a digit, underscore or alphabetic character, equivalent to [A-Za-z0-9]
\W Any non-word character, equivalent to [^A-Za-z0-9_]
. any character except newline

   Quantifier description:

 symbol describe 
{n}  matches the previous item n times
 {n,} Match the previous item n times, or more times
{n,m} Match the previous item at least n times, but not more than m times 
* Matches the previous item 0 or more times, equivalent to {0,} 
+ Matches the previous item 1 or more times, equivalent to {1,}
 ? Matches the previous item 0 or 1 times, that is, the previous item is optional

  symbol:

 symbol describe 
| Match any one of the selected characters, such as p|q, can match q or p
 () grouping
[] data to match
^ Negate

 

3. Common patterns of regular expressions

 1. g means global match . Without g, it means that only the first string that meets the requirements is matched;       

        "aaa".replace(/a/,"*"); --> "*aa"

       "aaa".replace(/a/g,"*"); --> "***"  

 

 2、i 表示忽略大小写匹配,默认要求大小写也必须符合正则要求。

    "aAa".replace(/A/,"#"); --> "a#a"

         "aAa".replace(/A/i,"#"); --> "#Aa"

      "aAa".replace(/A/gi,"#"); --> "###"

 

 3、m 表示多行匹配模式:

   如果不带m,表示一个字符串只有一个开头一个结尾;

   如果带m,那么对于多行字符串,可以有多个开头多个结尾。

 

例一:

  var str="This is an\n antzone good";   var reg=/an$/;   console.log(str.match(reg));   不能够匹配字符串"an",尽管"an"后面已经换行了,但是并没有采用多行匹配,所以不是字符串行的结尾。 例二:   var str="This is an\n antzone good";   var reg=/an$/m;   console.log(str.match(reg));
  可以匹配字符串
"an",因为采用了多行匹配。 例三:   var reg = /^b/;   var str = 'test\nbbs';   execReg(reg,str);
  匹配失败,因为字符串的开头没有b字符。但是加上m修饰符之后: 例四:   
var reg = /^b/m;   var str = 'test\nbbs';   execReg(reg,str);
  匹配到b,因为加了m修饰符之后,
^已经表示行首,由于bbs在字符串第二行的行首,所以可以成功地匹配。

 

   [多行字符串]

    ① 字符串中用\n表示换行:"abc\nabc".replace(/^a/gm,"*");

    ② ES6中可以用反引号``表示字符串,这种字符串,支持直接换行。

 

 4、检测方法

 (1)reg.test(str):检测一个字符串是否符合正则要求,返回true或false。

 

 (2)reg.exec(str):检测一个字符串是否符合正则要求,符合返回数组,不符合返回null。

           返回数组的格式:

    ① index属性: 表示字符串中,第几个字符,开始匹配正则。

    ② input属性: 表示完整的被检索字符串。

    ③ 下标第0个: 表示 符合正则要求的 字符串子串。

    ④ 下标从1往后: 表示 匹配正则中()包裹的字符串子串。也就是说,正则中有几个(), 返回的数组中就有几个下标。

      例:

      /12(3)(\d)56/.exec("aaa123056bbb"); 

     [
       0:"123056",

       1:"3",

       2:"0",

       index:3,

       input:"aaa123056bbb",

       length:3

      ]

 

四、正则表达式常用验证练习

 1、验证邮箱

var reg = /^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]+(\.[a-zA-Z]{2,3})?$/;

var mail = "[email protected]";

console.log(reg.test(mail));

 2、验证邮政编码(6位)

var reg = /^\d{5}$/;

var mailno = "251400";

console.log(reg.test(mailno));

 3、验证手机号(第一位为1,第二位为3/5/7/8,共11位)

var reg = /^1(3|5|7|8)\d{9}$/;

var phone = "17862009622";

console.log(reg.test(phone));

 4、验证年龄(1~120)

var reg = /^(\d|[1-9]\d|1[01]\d|120)$/;
// var reg = /^((1[0-1]|[1-9])?\d|120)$/;

var age = "121";

console.log(reg.test(age));

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297919&siteId=291194637