zoukankan      html  css  js  c++  java
  • JS字符串常用方法(自)---9、字符串匹配

    JS字符串常用方法(自)---9、字符串匹配

    一、总结

    一句话总结:

    字符串匹配方法有match(regexp)和matchAll(regexp),区别是matchAll()方法返回的是一个迭代器
    match(regexp)
    作用:用正则表达式匹配字符串
    参数:regexp(正则表达式)
    返回值:返回匹配的结果
    
    //match基本使用
    var str = 'For more information, see Chapter 3.4.5.1';
    var re = /see (chapter d+(.d)*)/i;
    var found = str.match(re);
    
    console.log(found);
    
    // logs [ 'see Chapter 3.4.5.1',
    //        'Chapter 3.4.5.1',
    //        '.1',
    //        index: 22,
    //        input: 'For more information, see Chapter 3.4.5.1' ]
    
    // 'see Chapter 3.4.5.1' 是整个匹配。
    // 'Chapter 3.4.5.1' 被'(chapter d+(.d)*)'捕获。
    // '.1' 是被'(.d)'捕获的最后一个值。
    // 'index' 属性(22) 是整个匹配从零开始的索引。
    // 'input' 属性是被解析的原始字符串。

    1、match()和matchAll()的区别?

    matchAll()方法返回的是一个迭代器,match()方法返回的是一个数组
    let regexp = /t(e)(st(d?))/g;
    let str = 'test1test2';
    let ans=str.matchAll(regexp);
    console.log(ans);//RegExp String Iterator
    
    // for (let val of  ans){
    //     console.log(val);
    // }
    
    let array = [...str.matchAll(regexp)];
    
    console.log(array[0]);
    // expected output: Array ["test1", "e", "st1", "1"]
    
    console.log(array[1]);
    // expected output: Array ["test2", "e", "st2", "2"]

    二、字符串匹配

    博客对应课程的视频位置:

    1、match(regexp)

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>match()</title>
     6 </head>
     7 <body>
     8 <!--
     9 
    10 match(regexp)
    11 作用:用正则表达式匹配字符串
    12 参数:regexp(正则表达式)
    13 返回值:返回匹配的结果
    14 
    15 -->
    16 <script>
    17     //match基本使用
    18     // var str = 'For more information, see Chapter 3.4.5.1';
    19     // var re = /see (chapter d+(.d)*)/i;
    20     // var found = str.match(re);
    21     //
    22     // console.log(found);
    23 
    24     // logs [ 'see Chapter 3.4.5.1',
    25     //        'Chapter 3.4.5.1',
    26     //        '.1',
    27     //        index: 22,
    28     //        input: 'For more information, see Chapter 3.4.5.1' ]
    29 
    30     // 'see Chapter 3.4.5.1' 是整个匹配。
    31     // 'Chapter 3.4.5.1' 被'(chapter d+(.d)*)'捕获。
    32     // '.1' 是被'(.d)'捕获的最后一个值。
    33     // 'index' 属性(22) 是整个匹配从零开始的索引。
    34     // 'input' 属性是被解析的原始字符串。
    35 
    36     //match 使用全局(global)和忽略大小写(ignore case)标志
    37     var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    38     var regexp = /[A-E]/gi;
    39     var matches_array = str.match(regexp);
    40 
    41     console.log(matches_array);// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
    42 
    43 </script>
    44 </body>
    45 </html>
     

    2、matchAll(regexp)

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>matchAll()</title>
     6 </head>
     7 <body>
     8 <!--
     9 matchAll(regexp)
    10 作用:matchAll() 方法返回一个包含所有匹配正则表达式的结果及分组捕获组的迭代器
    11 参数:regexp(正则表达式)
    12 返回值:返回匹配的结果
    13 
    14 match()和matchAll()的区别
    15 matchAll()方法返回的是一个迭代器,match()方法返回的是一个数组
    16 
    17 
    18 -->
    19 <script>
    20     let regexp = /t(e)(st(d?))/g;
    21     let str = 'test1test2';
    22     let ans=str.matchAll(regexp);
    23     console.log(ans);//RegExp String Iterator
    24 
    25     // for (let val of  ans){
    26     //     console.log(val);
    27     // }
    28 
    29     let array = [...str.matchAll(regexp)];
    30 
    31     console.log(array[0]);
    32     // expected output: Array ["test1", "e", "st1", "1"]
    33 
    34     console.log(array[1]);
    35     // expected output: Array ["test2", "e", "st2", "2"]
    36 
    37 </script>
    38 </body>
    39 </html>
     
  • 相关阅读:
    day31-python之内置函数
    day30-python之socket
    day28-python之property
    day27-python之迭代器协议
    day26-python之封装
    day25-python之继承组合
    初识AJAX
    写博客的心得
    web前端常见面试题
    学习网络安全的网站
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/12691419.html
Copyright © 2011-2022 走看看