什么是正则表达式?

正则表达式是由一个字符序列造成的搜寻模式。当你在文本中搜寻数据时,你能够用搜寻模式来形容你要查问的内容。正则表达式能够是一个简略的字符,或一个更简单的模式。正则表达式可用于所有文本搜寻和文本替换的操作。

正则表达式创立

  1. 字面量(间接量)
// 在一对反斜线中写正则表达式内容,如/abc/// 正则表达式外面不须要加引号 不论是数字型还是字符串型var pattern=/正则表达式/修饰符;var pattern=/qwer/igm;
  1. 构造函数
//结构正则表达式的实例,如new RexExp('abc')//外部传入的参数为字符串/字符串的变量var reg =new RegExp("正则表达式","修饰符")var reg =new RegExp("hello","g");

字符的分类

一般字符

  字母、数字、下划线、汉字、没有非凡含意的符号(,;!@等)

  实际上不是特殊字符的字符都是一般字符

特殊字符

  \:将特殊字符本义成一般字符

模式修饰符

  i:ignoreCase,匹配时漠视大小写

  m:multiline,多行匹配

  g:global,全局匹配

  字面量创立正则时,模式修饰符写在一对反斜线后

正则表达式实例办法

  1. exec---->>可用来匹配字符串中合乎正则表达式的字符串

如果匹配到,返回值是一个result数组:[匹配的内容,index: 在str中匹配的起始地位,input: 参数字符串,groups: undefined],r如果匹配不到则返回null

例如:

var str = 'hello world hello';var reg1 = /hello/;var reg2 = /hello/g;  //g示意全局匹配,也就是会匹配到str中所有满足正则表达式条件的字段var reg3 = /exe/g;console.log(reg1.exec(str)); //[ 'hello', index: 0, input: 'hello world hello', groups: undefined ]console.log(reg2.exec(str)); //[ 'hello', index: 0, input: 'hello world hello', groups: undefined ]console.log(reg3.exec(str)); // null     //str中并不存在exe字段,所以输入null// 如果是全局模式的正则验证 还能够应用循环进行输入var reg = /hello/g;var str = 'hello world hello hello good';while(true)    var result = reg.exec(str);    if(!result){        break;    }    console.log(result[0],result["index"],reg.lastIndex);  //hello 18 23}

须要留神的中央就是

1)如果正则表达式中有修饰符"g",这时,在正则表达式的实例reg中会保护lastIndex属性,记录下一次开始的地位,当第二次执行exec的时候,从lastIndex开始检索。
2)如果正则表达式中没有修饰符"g",不会保护lastIndex属性,每次执行从开始地位检索

2.test ---->> 用来测试待检测的字符串中是否有能够匹配到正则表达式的字符串,如果有返回true,否则返回false

例如:

var str = 'hello world';var reg1 = /world/;var reg2 = /Regex/;console.log(reg1.test(str)); //返回trueconsole.log(reg2.test(str)); //返回false

其实我比拟喜爱把字符串str放在test()更好看(直观校对)哈~~~

var reg1 = /world/;var reg2 = /Regex/;console.log(reg1.test(hello world)); //返回trueconsole.log(reg2.test(hello world)); //返回false//由后果可知//字符串中是否有能够匹配到正则表达式的字符串world,所以返回true

3.toString/toLocaleString --->> 把正则表达式的内容转化成字面量模式字符串/有本地特色的字符串(JS中没成果)

例如:

var reg1 = /hello/;console.log(reg1.toString()); //   /hello/    stringconsole.log(reg1.toLocaleString()); //   /hello/   string

4.valueOf ------>> 返回一个 Number 对象的根本数字值

它的作用是返回正则表达式自身

例如:

var reg1 = /hello/;console.log(reg1.valueOf());  //    /hello/      返回正则表达式自身

正则表达式实例属性

  1. lastIndex
    在菜鸟教程的解析为astIndex 属性用于规定下次匹配的起始地位,该属性只有设置标记 g 能力应用。

所以,当没设置全局匹配时,该属性值始终为0

设置了全局匹配时,每执行一次exec/test来匹配,latIndex就会移向匹配到的字符串的下一个地位,当指向的地位后没有能够再次匹配的字符串时,下一次执行exec返回null,test执行返回false,而后lastIndex归零,从字符串的结尾从新匹配一轮

能够了解成,每次正则查找的终点就是lastIndex

简而言之,全局匹配字符 g 对lastindex起到保护的作用,没有 g ,lastindex就没有作用

例如:

var reg =new RegExp('hello')var reg1 = /hello/g;var str = 'hello hello hello';  //字符串最初一个数的前面存在一个空字符'hello hello hello""'console.log('正则表达式构造函数');console.log(reg.lastIndex); //0console.log(reg.exec(str)); //返回第一个helloconsole.log(reg.lastIndex); //0console.log('正则表达式字面量');console.log(reg1.lastIndex); //0console.log(reg1.exec(str)); //[ 'hello', index: 0, input: 'hello hello hello', groups: undefined ]  第一个hello//每执行一次exec/test来匹配,latIndex就会移向匹配到的字符串的下一个地位  所以以后lastIndex正好指向空格console.log(reg1.lastIndex); //5console.log(reg1.lastIndex); //5console.log(reg1.exec(str)); //返回第二个hello   [ 'hello', index: 6, input: 'hello hello hello', groups: undefined ]//每执行一次exec/test来匹配,latIndex就会移向匹配到的字符串的下一个地位  所以以后lastIndex正好指向空格console.log(reg1.lastIndex); //11console.log(reg1.lastIndex); //11console.log(reg1.exec(str)); //返回第三个hello   [ 'hello', index: 12, input: 'hello hello hello', groups: undefined ]//每执行一次exec/test来匹配,latIndex就会移向匹配到的字符串的下一个地位  所以以后lastIndex正好指向空格console.log(reg1.lastIndex); //17    //因为字符串最初一个数的前面存在一个空字符'hello hello hello""'console.log(reg1.exec(str));  //null// 当指向的地位后没有能够再次匹配的字符串时,从新开始找console.log(reg1.lastIndex); //0console.log(reg1.exec(str));  //返回第一个hello   [ 'hello', index: 0, input: 'hello hello hello', groups: undefined ]

2.ignoreCase、global、multiline------>>判断正则表达式中是否有疏忽大小写、全局匹配、多行匹配三个模式修饰符

例如:

var reg1 = /hello/igm;console.log(reg1.ignoreCase); //true   疏忽大小写console.log(reg1.global); //true  全局匹配console.log(reg1.multiline);  //true   多行匹配

3.source --->> 返回字面量模式的正则表达式(相似于toString)

例如:

var reg1 = /hello/igm;console.log(reg1.source);  //hello   返回正则表达式自身

正则表达式语法-元字符

正则表达式中的所有字母和数字都是依照字面含意进行匹配的,Javascript正则表达式语法也反对非字母的字符匹配,这些字符须要通过反斜线\作为前缀进行本义。

  1. 间接量字符

  2. 字符汇合
    一个字符汇合,也叫字符组。匹配汇合中的任意一个字符。你能够应用连字符‘-’指定一个范畴

方括号用于查找某个范畴内的字符:

[abc] 查找方括号之间的任何字符

var str = 'abc qwe abd'var reg1 = /[abc]/;// 只有蕴含有a 或者 蕴含有b 或者蕴含有c 都返回为trueconsole.log(reg1.test(str)); //true

[0-9] 查找任何从0至9的数字

var str = 'abc qwe abd1'var reg1 = /[0-9]/igm;console.log(reg1.test(str)); //true

一个反义或补充字符集,也叫反义字符组。也就是说,它匹配任意不在括号内的字符。你也能够通过应用连字符 '-' 指定一个范畴内的字符。

留神^写在[]外面是补充字符集

//反义字符组//它匹配任意不在括号内的字符。你也能够通过应用连字符 '-' 指定一个范畴内的字符。var str = 'abc qwe abd1,2'console.log(str);var reg1 = /[^abc]/igm; //除了abc,可取其余任意字符console.log(reg1.exec(str)); //[ 'q', index: 4, input: 'abc qwe abd1,2', groups: undefined ]console.log(reg1.test(str)); //true //
  1. 边界符
  • ^ 匹配输出开始。示意匹配行首的文本(以谁开始)。如果多行(multiline)标记被设为 true,该字符也会匹配一个断行(line break)符后的开始处。

    例如:

// 以 ^ 开始// 必须是以hel结尾的字符串才会满足var reg = /^[hel]/;console.log(reg.exec(hello world hello Regexp));   // [ 'h', index: 0, input: 'hello world hello Regexp', groups: undefined ]console.log(reg.test(hello world hello Regexp)); //trueconsole.log(reg.test('aabcd')); // false
  • $ 匹配输出结尾。示意匹配行尾的文本(以谁完结)。如果多行(multiline)标记被设为 true,该字符也会匹配一个断行(line break)符的前的结尾处。

例如:

//  以 $ 完结// 必须是以exp结尾的字符串才会满足var reg1 = /[exp]$/;console.log(reg1.exec(hello world hello Regexp));// [//      'p',//      index: 23,//      input: 'hello world hello Regexp',//      groups: undefined//    ]console.log(reg1.test(hello world hello Regexp));  //trueconsole.log(reg.test('aabcd')); // false
  • 如果 ^和 $ 在一起,示意必须是准确匹配。
// 如果 ^和 $ 在一起,示意必须是准确匹配var reg2 = /^hello$/;console.log(reg2.test('hello'));  //trueconsole.log(reg2.test('helloeeee'));  //false

4.字符汇合与"^"和"$"一起应用

// 字符汇合与"^"和"$"一起应用// 三选一 只有是a 或者是 b  或者是c 这三个字母才返回 truevar rg1 = /^[abc]$/;     //单个匹配 console.log(rg1.test('aa'));//falseconsole.log(rg1.test('a'));//trueconsole.log(rg1.test('b'));//trueconsole.log(rg1.test('c'));//trueconsole.log(rg1.test('abc'));//false//26个英文字母任何一个字母返回 true  - 示意的是a 到z 的范畴  var reg = /^[a-z]$/console.log(reg.test('a'));//trueconsole.log(reg.test('z'));//trueconsole.log(reg.test('A'));//false//字符组合// 26个英文字母(大写和小写都能够)任何一个字母返回 truevar reg1 = /^[a-zA-Z0-9]$/;//取反 方括号外部加上 ^ 示意取反,只有蕴含方括号内的字符,都返回 false 。var reg2 = /^[^a-zA-Z0-9]$/;console.log(reg2.test('a'));//falseconsole.log(reg2.test('B'));//falseconsole.log(reg2.test(8));//falseconsole.log(reg2.test('!'));//trueconsole.log(reg2.test('?'),'其余字符'); //trueconsole.log(reg2.test(':'),'其余字符'); //trueconsole.log(reg2.test('='),'其余字符'); //true

\b 匹配一个零宽单词边界(zero-width word boundary),示意一个单词(而非字符)边界,也就是单词和空格之间的地位,或者字符(\w)与字符串结尾或者结尾之间的地位。

\B 匹配一个零宽非单词边界(zero-width non-word boundary),与"\b"相同。

例如:

//var str = 'Hello World Hello JavaScript';// \b   匹配一个零宽单词边界(zero-width word boundary),示意一个单词(而非字符)边界,// 也就是单词和空格之间的地位,或者字符(\w)与字符串结尾或者结尾之间的地位。var reg1 = /\bHello\b/g;// \B  匹配一个零宽非单词边界(zero-width non-word boundary),与"\b"相同var reg2 = /\BScrip\B/g;console.log(reg1.exec(str));// [//      'Hello',//      index: 0,//      input: 'Hello World Hello JavaScript',//      groups: undefined//    ]console.log(reg2.exec(str));// [//      'Scrip',//      index: 22,//      input: 'Hello World Hello JavaScript',//      groups: undefined//    ]