关于正则表达式:正则表达式知识点归纳

3次阅读

共计 6598 个字符,预计需要花费 17 分钟才能阅读完成。

什么是正则表达式?

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

正则表达式创立

  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)); // 返回 true
console.log(reg2.test(str)); // 返回 false

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

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

// 由后果可知
// 字符串中是否有能够匹配到正则表达式的字符串 world, 所以返回 true

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

例如:

var reg1 = /hello/;
console.log(reg1.toString()); //   /hello/    string
console.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); //0
console.log(reg.exec(str)); // 返回第一个 hello
console.log(reg.lastIndex); //0


console.log('正则表达式字面量');
console.log(reg1.lastIndex); //0
console.log(reg1.exec(str)); //['hello', index: 0, input: 'hello hello hello', groups: undefined]  第一个 hello
// 每执行一次 exec/test 来匹配,latIndex 就会移向匹配到的字符串的下一个地位  所以以后 lastIndex 正好指向空格
console.log(reg1.lastIndex); //5

console.log(reg1.lastIndex); //5
console.log(reg1.exec(str)); // 返回第二个 hello   ['hello', index: 6, input: 'hello hello hello', groups: undefined]
// 每执行一次 exec/test 来匹配,latIndex 就会移向匹配到的字符串的下一个地位  所以以后 lastIndex 正好指向空格
console.log(reg1.lastIndex); //11

console.log(reg1.lastIndex); //11
console.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); //0
console.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 都返回为 true
console.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)); //true
console.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));  //true
console.log(reg.test('aabcd')); // false
  • 如果 ^ 和 $ 在一起,示意必须是准确匹配。
// 如果 ^ 和 $ 在一起,示意必须是准确匹配
var reg2 = /^hello$/;
console.log(reg2.test('hello'));  //true
console.log(reg2.test('helloeeee'));  //false

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

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

// 三选一 只有是 a 或者是 b  或者是 c 这三个字母才返回 true
var rg1 = /^[abc]$/;     // 单个匹配 
console.log(rg1.test('aa'));//false
console.log(rg1.test('a'));//true
console.log(rg1.test('b'));//true
console.log(rg1.test('c'));//true
console.log(rg1.test('abc'));//false

//26 个英文字母任何一个字母返回 true  - 示意的是 a 到 z 的范畴  
var reg = /^[a-z]$/
console.log(reg.test('a'));//true
console.log(reg.test('z'));//true
console.log(reg.test('A'));//false


// 字符组合
// 26 个英文字母 (大写和小写都能够) 任何一个字母返回 true
var reg1 = /^[a-zA-Z0-9]$/;

// 取反 方括号外部加上 ^ 示意取反,只有蕴含方括号内的字符,都返回 false。var reg2 = /^[^a-zA-Z0-9]$/;
console.log(reg2.test('a'));//false
console.log(reg2.test('B'));//false
console.log(reg2.test(8));//false
console.log(reg2.test('!'));//true
console.log(reg2.test('?'),'其余字符'); //true
console.log(reg2.test(':'),'其余字符'); //true
console.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
//    ]

 
正文完
 0