网络编程 
首页 > 网络编程 > 浏览文章

深入浅析js中的正则表达式

(编辑:jimmy 日期: 2024/10/10 浏览:3 次 )

阅读目录

  • 正则表达式的创建
  • 正则表达式中的特殊字符
  • \ (反斜杠)
  • ^
  • $
  • *,  +,  .(小数点)
  • "color: #ff0000">正则表达式的创建

    两种方法,一种是直接写,由包含在斜杠之间的模式组成;另一种是调用RegExp对象的构造函数。

    两种方法的创建代码如下:

    // 直接创建
    const regex1 = /ab+c/;
    const regex2 = /^[a-zA-Z]+[0-9]*\W"gi");
    const regex5 = new RegExp('^[a-zA-Z]+[0-9]*\W"htmlcode">
    
    const ncname = '[a-zA-Z_][\\w\\-\\.]*';
    const qnameCapture = '(("color: #ff0000">正则表达式中的特殊字符

    \ (反斜杠)

    1.在非特殊字符前加反斜杠表示下一个字符是特殊的;

    2.将其后的特殊字符转译为字面量;

    注意:在使用RegExp构造函数时要将\转译,因为\在字符串里也是转译字符

    ^

    1.匹配输入的开始;

    2.在[]中的第一位时表示反向字符集;

    例子:

    /^A/.exec('an A')    // null
    /^A/.exec('An E')    // ["A", index: 0, input: "An E"]

    $

    匹配输入的结束

    /t$/.exec('eater')    // null
    /t$/.exec('eat')     // ["t", index: 2, input: "eat"]
    *, +, .(小数点) 

    *:匹配前一个表达式0次或多次。等价于 {0,};

    +:匹配前面一个表达式1次或者多次。等价于 {1,};

    .:

    匹配除换行符之外的任何单个字符;

    "htmlcode">

    /\d+/.exec('123abc')       // ["123", index: 0, input: "123abc"]
    /\d+"1", index: 0, input: "123abc"]

    (x)

    匹配 'x' 并且记住匹配项,括号表示捕获括号;

    例子:

    /(foo) (bar) \1 \2/.test('bar foo bar foo');  // false
    /(bar) (foo) \1 \2/.test('bar foo bar foo');  // true
    /(bar) (foo) \1 \2/.test('bar foo');      // false
    /(bar) (foo) \1 \2/.test('bar foo foo bar');  // false
    /(bar) (foo) \2 \1/.test('bar foo foo bar');  // true
    'bar foo bar foo'.replace( /(bar) (foo)/, '$2 $1' );  // "foo bar bar foo"

    模式 /(foo) (bar) \1 \2/ 中的 '(foo)' 和 '(bar)' 匹配并记住字符串 "foo bar foo bar" 中前两个单词。模式中的 \1 和 \2 匹配字符串的后两个单词。

    注意:\1、\2、\n 是用在正则表达式的匹配环节,在正则表达式的替换环节,则要使用像 $1、$2、$n 这样的语法。例如,'bar foo'.replace( /(...) (...)/, '$2 $1' )。

    ("htmlcode">

    'foo'.match(/foo{1,2}/)        // ["foo", index: 0, input: "foo"]
    'foo'.match(/("foo", index: 0, input: "foo"]
    'foofoo'.match(/("foofoo", index: 0, input: "foofoo"]
    'foofoo'.match(/foo{1,2}/)       // ["foo", index: 0, input: "foofoo"]

    使用场景:示例表达式 /("htmlcode">

    'JackSprat'.match(/Jack("Jack", index: 0, input: "JackSprat"]
    'JackWprat'.match(/Jack("Jack", index: 0, input: "JackWprat"]
    /\d+("3.141")    // ["141", index: 2, input: "3.141"]

    {n}, {n,m}:

    {n}:匹配了前面一个字符刚好发生了n次;

    {n,m}:匹配前面的字符至少n次,最多m次。如果 n 或者 m 的值是0, 这个值被忽略;

    例子:

      /a{2}/.exec('candy')     // null
      /a{2}/.exec('caandy')    // ["aa", index: 1, input: "caandy"]
      /a{2}/.exec('caaandy')    // ["aa", index: 1, input: "caaandy"]
      /a{1,3}/.exec('candy')    // ["a", index: 1, input: "candy"]
      /a{1,3}/.exec('caandy')   // ["aa", index: 1, input: "caandy"]
      /a{1,3}/.exec('caaandy')   // ["aaa", index: 1, input: "caaandy"]
      /a{1,3}/.exec('caaaandy')  // ["aaa", index: 1, input: "caaaandy"]  

    [xyz], [^xyz]

    [xyz]:一个字符集合。匹配方括号的中任意字符;

    [^xyz]:一个反向字符集。匹配任何没有包含在方括号中的字符;

    这两种匹配都可以使用破折号(-)来指定一个字符范围,特殊符号在字符集中没有了特殊意义。

    例:

    function escapeRegExp(string){
      return string.replace(/([.*+"\\$&"); 
      //$&表示整个被匹配的字符串
    }

    例子中的.*+"htmlcode">

      /\bm/.exec('moon')             // ["m", index: 0, input: "moon"]
      /\bm/.exec('san moon')           // ["m", index: 4, input: "san moon"]
      /oo\b/.exec('moon')             // null
      /\B../.exec('noonday')          // ["oo", index: 1, input: "noonday"]
      /y\B../.exec('possibly yesterday')    // /y\B../.exec('possibly yesterday')

    \d:匹配一个数字,等价于[0-9];

    \D:匹配一个非数字字符,等价于[^0-9];

    \f:匹配一个换页符 (U+000C);

    \n:匹配一个换行符 (U+000A);

    \r:匹配一个回车符 (U+000D);

    \s:匹配一个空白字符,包括空格、制表符、换页符和换行符,等价于[ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff];

    \S:匹配一个非空白字符,等价于[^ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff];

    \w:匹配一个单字字符(字母、数字或者下划线),等价于[A-Za-z0-9_];

    \W:匹配一个非单字字符,等价于[^A-Za-z0-9_];

    正则表达式标志

    g:全局搜索;

    i:不区分大小写;

    m:多行搜索;

    正则表达式使用

    RegExp有exec()和test()方法;

    exec匹配的结果为:匹配结果、捕获结果,index和input。

    test匹配的结果为true或false,效率比exec要高。

    String有match(),replace(),search(),split()方法;

    match匹配的结果同RegExp的exec,replace根据正则表达式替换,search查找所以位置,split根据正则表达式分割字符串。

    其中,当replace有function时,参数说明如下:

    * 匹配项
    * 记忆项(括号里面的项)
    * ...
    * 匹配的index
    * input输入项

    以上所述是小编给大家介绍的js中的正则表达式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

    上一篇:正则表达式验证用户名、密码、手机号码、身份证(推荐)
    下一篇:浅析一个匹配数字和字母密码的正则表达式
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。