正则表达式

30、正则表达式   是一个描述字符模式的对象
 
          ① 正则表达式对象的创建(JavaScript的用法)
                     构造函数 第一个参数包括正则表达式的主体部分,即正则表达式直接量中两条斜线之间的 文本,第二个参数指定正则表达式的修饰符。只能传入g ,i,m或者其组合,可以省略
var pattern =new RegExp("正则表达式","修饰符")
var pattern =new RegExp("hello","ig");
 
                    正则表达式字面量
var pattern = /正则表达式/修饰符;
var pattern = /hello/ig; 
 
修饰符
i        ignore case 不区分大小写
g       global 全局
m      multiline 多行
 
② 原型属性 
RegExp.prototype.global 布尔值,表明这个正则表达式是否带有修饰符g
 console.log(pattern.global);  //true
 
RegExp.prototype.ignoreCase 布尔值,表明这个正则表达式是否带有修饰符i
 console.log(pattern.ignoreCase );  //true
 
RegExp.prototype.multiline 布尔值,表明这个正则表达式是否带有修饰符m
 console.log(pattern.multiline );  //false
 
RegExp.prototype.lastIndex 如果匹配模式带有g,这个属性存储在整个字符串中下 一次检索的开始位置,这个属性会被exec(), test()方法调用到
 //下一次检索的位置,如果有g修饰,会维护lastIndex,如果没有g修饰,lastIndex永远都是0
console.log(pattern.lastIndex);     //0
 
RegExp.prototype.source 包含正则表达式文本
                    console.log(pattern.source );  //hello
 
③ 原型方法 
RegExp.prototype.test()
var result = pattern.test(str); 检测一个字符串是否匹配某个模式 
参数:字符串 
返回值:布尔类型 true代表有符合条件的,false代表没有符合条件的
 
var str = 'heLLoworld helloloworld heLLoloworld ';
//找hello字符串,如果有就返回true,没有则返回false
var pattern = /hello/ig;
//进行模式匹配
console.log(pattern.lastIndex);  //0    //如果没有匹配到,值还是为0
var result = pattern.test(str);
console.log(result);   //true          //如果没有匹配到,值为false
console.log(pattern.lastIndex);  //5    //如果没有匹配到,值还是为0
 
 
RegExp.prototype.exec()
var result = pattern.exec(str) 检索字符串中的正则表达式的匹配 
参数 :字符串 
返回值 :数组或者null 数组        数组里只有一个匹配到的值
 
如果正则表达式中有修饰符"g",这时,在pattern中会维护lastIndex属性,记录下一次开始的位置,当第二次执行exec的时候,从lastIndex开始检索。 如果正则表达式中没有修饰符"g",不会维护lastIndex属性,每次执行从开始位置检索
          var str = 'heLLoworld helloloworld heLLoloworld ';
          var pattern = /hello/ig;
//exec   获取匹配到的字符串,返回包含该字符串的数组,如果没有找到,返回null
var result = pattern.exec(str);   //['heLLo' ,index: 0, input: 'heLLoworld helloloworld heLLoloworld '              //如果没有匹配到,返回null
console.log(result.length); //1
console.log(result.index);   //0
console.log(result.input);   //'heLLoworld helloloworld heLLoloworld '
 
//将匹配到的所有的字符串拿出来,放到一个数组中
var arr = [];
while(result = pattern.exec(str)){
  console.log(result);
  arr = arr.concat(result);
}
console.log(arr);  //['heLLo', 'hello'] 这里会去重
 
RegExp.prototype.toString()
 
 
④Javascript中 String对正则表达式的支持
search()
参数为一个正则表达式。如果参数不为正则表达式,则先通过RegExp将其转换为构造函数。不支持全局检索,返回第一个与之匹配的子串的位置,如果找不到匹配的子串,返回-1。类似于正则表达式的 test 方法
  var str = 'heLLoworld helloloworld heLLoloworld ';
       var pattern = /hello/ig;
  var res = str.search(pattern);
       console.log(res);    //0
 
match()
最常用的正则表达式方法,参数为正则表达式。返回由匹配结果组成的数组或者null。当正则表达式中没有g修饰符的时候,就不是全局匹配。这时,数组的第一个元素就为匹配的字符串,剩余的元素则是由正则表达式中用圆括号括起来的子表达式。如果该正则表达式设置为修饰符g, 则该方法返回的数组包含字符串中所有匹配结果。类似于正则表达式的 exec 方法
"1 plus 2 equals 3".match(/\d+/g) //返回["1","2","3"]
 
    var str = 'heLLoworld helloloworld heLLoloworld ';
          var pattern = /hello/ig;
          var arr = [];
          arr = str.match(pattern);
          console.log(arr);   //['heLLo', 'hello', 'heLLo'];    //这里不会去重
 
replace()
用以执行检索和替换操作。第一个参数是正则表达式,第二个参数是要替换的字符串。 text.replace(/javascript/gi,“JavaScript”); //不区分大小写将所有javascript转换为JavaScript 
        var str = 'hello hello hello world';
        var result = str.replace(/hello/ig, 'JavaScript');
        console.log(result, str);      //'JavaScript JavaScript JavaScript world' 'hello hello hello world'
 
split()
将字符串转成数组。参数可以为正则表达式 "1, 2, 3, 4, 5".split(/\s*,\s*/); //["1","2","3","4","5"] 允许分隔符左右两边留有空白
var str = '12&34&56'
console.log(str.split('&'));   //['12','34','56']
console.log(str.split(/&/));   //['12','34','56']
 
⑤正则表达式的模式
字符类 [直接量]
. (点号,小数点) 匹配任意单个字符,但是行结束符除外
\d 匹配任意阿拉伯数字。等价于[0-9]
\D 匹配任意一个不是阿拉伯数字的字符。等价于[^0-9]。
\w 匹配任意来自基本拉丁字母表中的字母数字字符,还包括下划线。等价于 [A-Za-z0-9_]。
\W 匹配任意不是基本拉丁字母表中单词(字母数字下划线)字符的字符。等价于 [^A-Za-z0- 9_]。
\s 匹配一个空白符,包括空格、制表符、换页符、换行符和其他 Unicode 空格。
\S 匹配一个非空白符。
\t 匹配一个水平制表符(tab)
\r 匹配一个回车符(carriage return)
\n 匹配一个换行符(linefeed)
\v 匹配一个垂直制表符(vertical tab)
\f 匹配一个换页符(form-feed)
 
字符集合
[xyz] 一个字符集合,也叫字符组。匹配集合中的任意一个字符。你可以使用连字符‘-’指定一个 范围。[0-9] [a-z]
[^xyz] 一个反义或补充字符集,也叫反义字符组。也就是说,它匹配任意不在括号内的字符。你 也可以通过使用连字符 '-' 指定一个范围内的字符。
 
边界
^ 匹配输入开始。如果多行(multiline)标志被设为 true,该字符也会匹配一个断行(line break)符后的开始处。
$ 匹配输入结尾。如果多行(multiline)标志被设为 true,该字符也会匹配一个断行(line break)符的前的结尾处。
\b 匹配一个零宽单词边界(zero-width word boundary),如一个字母与一个空格之间。
\B 匹配一个零宽非单词边界(zero-width non-word boundary),如两个字母之间或两个空格之间。
 
var str = 'dfsd4343fdgedv';
var pattern = /^[a-z].*\d$/g;
console.log(str.match(pattern)); //null
分组
(x)      匹配 x 并且捕获匹配项。 这被称为捕获括号(capturing parentheses)。
\n       n 是一个正整数。一个反向引用(back reference),指向正则表达式中第 n 个括号(从 左开始数)中匹配的子字符串。引用的是最后一次匹配的结果,可以在不加g修饰的match中的数组中除了第一个之外的其他元素中拿到
     例如:/\w+:\/\/\w+(\.)\w+\1\w+/
 
数量词
x* 匹配前面的模式 x 0 或多次。
x+ 匹配前面的模式 x 1 或多次。等价于 {1,}。
x*? 像上面的 * 一样匹配前面的模式 x,然而匹配是最小可能匹配。
x+? 像上面的 + 一样匹配前面的模式 x,然而匹配是最小可能匹配。
x? 匹配前面的模式 x 0 或 1 次。
x|y 匹配 x 或 y
x{n} n 是一个正整数。前面的模式 x 连续出现 n 次时匹配
x{n,} n 是一个正整数。前面的模式 x 连续出现至少 n 次时匹配。
x{n,m} n 和 m 为正整数。前面的模式 x 连续出现至少 n 次,至多 m 次时匹配。
 
 
//次数的后面加个?,表示非贪婪模式,取出匹配最少的字符串
var str = 'hello world javascript';
//贪婪模式和非贪婪模式,默认为贪婪模式,取出匹配最多的字符串
var pattern = /\w{3,5}/i;
console.log(pattern.exec(str));   //5
 
 
//验证电话号码
var str = '1245667554';
var pattern = /^\d{11}$/;
console.log(pattern.test(str));   //false
posted @ 2019-09-23 12:46  xsha_h  阅读(171)  评论(0编辑  收藏  举报