相关方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>相关方法</title>
</head>
<body>
   
    <script>

        /**
         *   \d 匹配一个数字         [0-9]
         *   \D 匹配一个非数字字符    [^0-9]
         *   \w 匹配一个单字字符 (字母、数字 或者下划线) [A-Za-z0-9_]
         *   \W 匹配一个非单字字符   [^A-Za-z0-9_]
         *   \s 匹配一个空白字符, 包括空格 制表符 和 换行符
         *   . 任意字符
         *   ^ 匹配开头
         *   $ 匹配结尾
         *
         *   new RegExp()写法 需要多写一个反斜杠
         *    例子  /^\d$/   new RegExp('^\\d$')
        */

         /**   量词
          *    * 匹配前一个表达式0次或者多次 等价于{0,}
          *    + 匹配前一个表达式1次或者多次 等价于{1,}
          *    ? 匹配前一个表达式0次或者1次 等价于{0,1}
          *   {n}  n 是一个正整数 匹配了前面一个字符刚好出现了n 次
          *   {n,} n 是一个正整数 匹配了前面一个字符至少出现了n 次
          *   {n,m} n 和 m 都是一个正整数 匹配前面的字符至少出现了n次,最多m次
          *   () 代表一个整体
         */

         /**  修饰符
          *   i 不区分大小写搜索
          *   g 全局搜索
         */


         /**
          * 相关方法
          *  test()  测试某字符串是否匹配正则表达式 返回布尔值
          *  exec()  根据正则表达式 在字符串中进行查找,返回结果数组或者 null
         */

        // var str = 'adc123def456ghi789'
        // var regexp = /\d+/
       
        // index 索引  input 原始字符串
        // ["123", index: 3, input: "adc123def456ghi789", groups: undefined]
        // console.log(regexp.exec(str));  

        // 有 ‘g' 修饰符的正则表达式将自动成为 ‘有状态’的, 可以对单个字符串中的多次匹配结果进行逐条的遍历

       // var regexp2 = /\d+/g
        // console.log(regexp2.exec(str));  // ["123", index: 3, input: "adc123def456ghi789", groups: undefined]
        // console.log(regexp2.exec(str));  // ["456", index: 9, input: "adc123def456ghi789", groups: undefined]
        // console.log(regexp2.exec(str));  // ["789", index: 15, input: "adc123def456ghi789", groups: undefined]
        // console.log(regexp2.exec(str));  // null
 

        // 循环遍历
        // var result
        // while(result = regexp2.exec(str)){
        //     console.log(result[0]);
        // }


        // 字符串相关方法
        /**
         *  search() 在字符串中根据正则表达式进行查找匹配 返回首次匹配到的索引位置索引 找不到就返回-1
         *  match() 在字符串中根据正则表达式进行查找匹配  返回一个数组 找不到就返回null
         *  replace() 使用替换字符串替换掉匹配的子字符串, 可以使用正则表达式
         *  split()  分隔字符串为数组 可以使用正则表达式
         *
        */

        var str = 'adc123def456ghi789'
        // search()
        // var result1 = str.search(/\d+/g)
        // var result2 = str.search(/\m/g)
        // console.log(result1);  // 3
        // console.log(result2);  // -1

        // match()
        // var result3 = str.match(/\d+/g)
        // console.log(result3);  // ["123", "456", "789"]

        // replace()
        // var result4 = str.replace(/[a-z]+/g,'*')  
        // console.log(result4);  // *123*456*789
 
        // split()
        var result5 = str.split(/\d+/g)  
        console.log(result5);  // ["adc", "def", "ghi", ""]

    </script>

</body>
</html>
posted @ 2021-12-18 23:18  13522679763-任国强  阅读(25)  评论(0)    收藏  举报