zoukankan      html  css  js  c++  java
  • js正则

    RegExp 是正则表达式的缩写。

    RegExp 对象用于存储检索模式。

    RegExp 对象的方法

    RegExp 对象有 3 个方法:test()、exec() 以及 compile()。

    test()

    test() 方法检索字符串中的指定值。返回值是 true 或 false。

    exec()

    exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

    compile()

    compile() 方法用于改变 RegExp。

    compile() 既可以改变检索模式,也可以添加或删除第二个参数。

    正则表达式中的特殊字符
          表示转义
       ^   表示匹配输入的开始
       $    表示匹配输入的结束
       *    一个表达式的多次或0次
       +   匹配前面一个表达式一次或多次
       ?    匹配前面一个表达式0次或者一次 ???
       .   匹配除换行符之外的任何单个字符
        [abc]   查找方括号之间的字符
        [^abc]   查找不在方括号之间的字符
        (a|b|c)   查找任何指定选项
        w   查找单词字符
        W   查找非单词字符
        d   查找数字字符

         D 查找非数字字符
        s   查找空字符

          S 查找非空白字符

     

    修饰符

    修饰符描述
    i 执行对大小写不敏感的匹配。
    g 执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)。
    m 执行多行匹配。

    方括号

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

    表达式描述
    [abc] 查找方括号之间的任何字符。
    [^abc] 查找任何不在方括号之间的字符。
    [0-9] 查找任何从 0 至 9 的数字。
    [a-z] 查找任何从小写 a 到小写 z 的字符。
    [A-Z] 查找任何从大写 A 到大写 Z 的字符。
    [A-z] 查找任何从大写 A 到小写 z 的字符。
    [adgk] 查找给定集合内的任何字符。
    [^adgk] 查找给定集合外的任何字符。
    (red|blue|green) 查找任何指定的选项。

    [  ]
       res = /[abc]/.test('lue');          false
       res = /[^abc]/.test('lue');        true
       res = /[0-9]/.test('this is a test');       false
       res = /[a-z]/.test('234235453245');      false
       res = /php|javascript|ios/i.test('PHP');      true
      console.log(res);

     

    元字符

    元字符(Metacharacter)是拥有特殊含义的字符:

    元字符描述
    . 查找单个字符,除了换行和行结束符。
    w 查找单词字符。
    W 查找非单词字符。
    d 查找数字。
    D 查找非数字字符。
    s 查找空白字符。
    S 查找非空白字符。
     匹配单词边界。
    B 匹配非单词边界。
    查找 NUL 字符。
    查找换行符。
    f 查找换页符。
    查找回车符。
    查找制表符。
    v 查找垂直制表符。
    xxx 查找以八进制数 xxx 规定的字符。
    xdd 查找以十六进制数 dd 规定的字符。
    uxxxx 查找以十六进制数 xxxx 规定的 Unicode 字符。

     元字符

       res = /./.test(' ');        false
       res = /./.test('this is a test');        true
       res = /w/.test('hello nana')    //[a-zA-Z0-9]        true
       res = /w/.test('!#@w');          true
       res = /W/.test('!#%9');     //[^a-zA-Z0-9]    true
       res = /s/.test('hello world');          true
       res = /S/.test(' ');            false
       res = /go/.test('good');            true   左边
       res = /o/.test('good');                false  右边
       res = /d/.test('good');            true             
       res = /oB/.test('good');          true           /B不区分左右边界
    console.log(res);

     

    量词

    量词描述
    n+ 匹配任何包含至少一个 n 的字符串。
    n* 匹配任何包含零个或多个 n 的字符串。
    n? 匹配任何包含零个或一个 n 的字符串。
    n{X} 匹配包含 X 个 n 的序列的字符串。
    n{X,Y} 匹配包含 X 至 Y 个 n 的序列的字符串。
    n{X,} 匹配包含至少 X 个 n 的序列的字符串。
    n$ 匹配任何结尾为 n 的字符串。
    ^n 匹配任何开头为 n 的字符串。
    ?=n 匹配任何其后紧接指定字符串 n 的字符串。
    ?!n 匹配任何其后没有紧接指定字符串 n 的字符串。

    量词

      res = /o+/.test('google');        true
       res = /o*/.test('google');        true
       res = /o?/.test('google');        true
       res = /o{2}/.test('goooogle');       true
       res = /o{1,3}/.test('goooogle');      true
       res = /^k/.test('king');          true
       res = /i$/.test('mai');          true
       res = /o(?=w)/.test('helloworld');      true
       res = /o(?!w)/.test('helloworld');      true
       res = /d/.test('aajkldsfj');     //[0-9]    false  
       res = /D/.test('sdfkjllsdfj');     //[^0-9]    true
       console.log(res);

    RegExp 对象属性

    属性描述FFIE
    global RegExp 对象是否具有标志 g。 1 4
    ignoreCase RegExp 对象是否具有标志 i。 1 4
    lastIndex 一个整数,标示开始下一次匹配的字符位置。 1 4
    multiline RegExp 对象是否具有标志 m。 1 4
    source 正则表达式的源文本。 1 4
     
     
     

    RegExp 对象方法

    方法描述FFIE
    compile 编译正则表达式。 1 4
    exec 检索字符串中指定的值。返回找到的值,并确定其位置。 1 4
    test 检索字符串中指定的值。返回 true 或 false。 1 4

    res = /is/i.exec('this is a test');
    console.log(res);
    console.log(res[0]);
    var str = 'this is a test hello nana hello world';
    var patt = /i/ig;
    var myArr;

    while((myArr = patt.exec(str)) !== null) {
    var msg = '找到了' + myArr[0] + '!';
    msg += '下一个匹配从' + patt.lastIndex;
    console.log(msg);
    }

    ["is", index: 2, input: "this is a test", groups: undefined]
    8-RegExp.html:56 is
    8-RegExp.html:64 找到了i!下一个匹配从3
    8-RegExp.html:64 找到了i!下一个匹配从6

    支持正则表达式的 String 对象的方法

    方法描述FFIE
    search 检索与正则表达式相匹配的值。 1 4
    match 找到一个或多个正则表达式的匹配。 1 4
    replace 替换与正则表达式匹配的子串。 1 4
    split 把字符串分割为字符串数组。 1 4

    var str = 'this is a test';
    res = str.match(/IS/i);
    console.log(res);                            ["is", index: 2, input: "this is a test", groups: undefined]

    res = str.match(/IS/ig);
    console.log(res);                (2) ["is", "is"]

    res = str.search(/is/i);
    console.log(res);          2

    var str1 = str.replace(/is/ig, '!');
    console.log(str1);                       th! ! a test

    var str = '2015-09-27';
    res = str.replace(/(d{4})-(d{2})-(d{2})/, '$2/$3/$1');
    console.log(res);                      09/27/2015

    .Email正则
       汉字在正则中表示为[u4e00-u9fa5]
       一般域名的规律为“[N级域名][三级域名.]二级域名.顶级域名”,比如“qq.com”、“www.qq.com”、“mp.weixin.qq.com”、“12-34.com.cn”,
       分析可得域名类似“xx .xx .xx .xx”组成。

       “xx”部分可以表示为[a-zA-Z0-9_-]+
       “.xx”部分可以表示为.[a-zA-Z0-9_-]+
       多个“.xx”可以表示为(.[a-zA-Z0-9_-]+)+

  • 相关阅读:
    (1)---(10)小结
    Java网络编程从入门到精通(10):Inet4Address类和Inet6Address类
    Java网络编程从入门到精通 (9):使用isXxx方法判断地址类型
    Java网络编程从入门到精通(8):用getAddress方法获得IP地址
    Java网络编程从入门到精通(7):用getHostAddress方法获得IP地址
    Java网络编程从入门到精通(6):使用getCanonicalHostName方法获得主机名
    javaEE中各种Context及其相关的总结(servletContext、httpServletRequest、pageContext、ActionContext、servletActionContext)
    子表,父表;一对多,多对一;主键,外键梳理。
    eclipse中js文件的默认编码格式修改。
    Eclipse/MyEclipse一次修改或者编辑多行的快捷键
  • 原文地址:https://www.cnblogs.com/ysboke/p/9617201.html
Copyright © 2011-2022 走看看