Skip to content

Latest commit

History

History
169 lines (116 loc) · 3.73 KB

File metadata and controls

169 lines (116 loc) · 3.73 KB

正则表达式

快速导航

模式修饰符参数

  • i:忽略大小写
  • g:全局匹配
  • m:多行匹配
  • /hello/:两个反斜杠是正则表达式的字面量表示法

两个测试方法

  • test
consttest=newRegExp('hello world','ig');console.log(test.test('hello world'));// true
  • exec

返回的是数组,有就返回数组的值,没有返回为null。

consttest=newRegExp('hello world','ig');console.log(test.exec('hello'));// null

4个正则表达式方法

  • match(pattern)

将所有匹配的字符串组合成数组返回

constpattern=/Box/ig;conststr="This is a Box! The is a box!";console.log(str.match(pattern));
  • search(pattern)

返回字符串中pattern开始位置,忽略全局匹配

constpattern=/Box/i;//conststr="This is a Box! The is a box!";console.log(str.search(pattern));// 10
  • replace(pattern)

替换匹配到的字符串

constpattern=/Box/ig;conststr="This is a Box! The is a box!";console.log(str.replace(pattern,'Tom'));
  • split(pattern)

返回字符串指定pattern拆分数组

constpattern=//ig;//空格conststr="This is a Box! The is a box!";console.log(str.split(pattern));//以空格进行分割,返回的是数组// 输出结果// [ 'This', 'is', 'a', 'Box!', 'The', 'is', 'a', 'box!' ]

匹配模式

  • \w表示a-zA-Z_

  • 锚元字符匹配(^ $)^强制收匹配$强制尾匹配,并且只匹配一个

constpattern=/^[a-z]oogle\d$/;conststr="aoogle2";console.log(pattern.test(str));// true

注意:^符号在[]里面表示 非 在外边表示强制首匹配,并且只匹配一个 要想匹配多个值,使用+

  • \b表示到达边界

  • |表示匹配或选择模式

constpattern=/baidu|google|bing/;//匹配或选择其中某个字符,不是相等,包含的意思conststr="baidu a google";console.log(pattern.test(str));//返回true

常用正则表达式

  • 检查邮政编码
constpattern=/^[1-9]{1}[0-9]{5}$/;conststr="122534";//共6位数,第一位不能为0console.log(pattern.test(str));// true
  • 压缩包后缀名

\w等于a-zA-Z0-9_ 使用^限定从首字母匹配 .是特殊符号需要\n进行转义 |选择符必须使用()进行分组

constpattern=/^[\w]+\.(zip|gz|rar)$/;conststr="a12_.zip";//文件名 字母_数字.zip,gz,rarconsole.log(pattern.test(str));// true
  • 删除多余空格

方法一:使用replace只匹配一个,所以使用+匹配多个

varpattern=/^\s+/;varstr=" google ";varresult=str.replace(pattern,'');pattern=/\s+$/;result=result.replace(pattern,'');console.log('|'+result+'|');// |google|

方法二:(.+)贪婪模式,使用惰性模式,后面的空格不让匹配

varpattern=/^\s+(.+?)\s+$/;varstr=" google ";varresult=pattern.exec(str,'')[1];console.log('|'+result+'|');

方法三:(.+)贪婪模式,改为惰性模式,使用分组模式,只取匹配的内容

varpattern=/^\s+(.+?)\s+$/;varstr=" google ";varresult=str.replace(pattern,'$1');//使用分组模式console.log('|'+result+'|');// |google|
  • 简单邮箱验证
varpattern=/^([\w\.\_]+)@([\w\_]+)\.([a-zA-Z]){2,4}$/;varstr="qzfweb@gmail.com";console.log(pattern.test(str));// true