js正则表达式之match函数

功能:使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回 

函数格式:stringObj.match(rgExp) stringObj为字符串必选 rgExp为正则表达式必选项 

返回值:如果能匹配则返回结果数组,如果不能匹配返回null 

使用方法: 
stringObj.match(rgExp) 
其中stringObj是必选项。对其进行查找的 String 对象或字符串文字。 
rgExp是必选项。为包含正则表达式模式和可用标志的正则表达式对象。也可以是包含正则表达式模式和可用标志的变量名或字符串文字。 
如果js中match函数方法没有找到匹配,返回 null。如果找到匹配返回一个数组并且更新全局 RegExp 对象的属性以反映匹配结果。JavaScript中match函数方法返回的数组有三个属性:input、index和lastIndex。Input 属性包含整个的被查找字符串。Index 属性包含了在整个被查找字符串中匹配的子字符串的位置。LastIndex 属性包含了最后一次匹配中最后一个字符的下一个位置。如果没有设置全局标志 (g),数组的0元素包含整个匹配,而第 1 到 n 元素包含了匹配中曾出现过的任一个子匹配。这相当于没有设置全局标志的 exec 方法。如果设置了全局标志,元素0到n中包含所有匹配。 

下面的示例演示了js中match函数方法的用法: 

function MatchDemo(){ 
var r, re; // 声明变量。 
var s = "The rain in Spain falls mainly in the plain"; 
re = /ain/i; // 创建正则表达式模式。 
r = s.match(re); // 尝试匹配搜索字符串。 
return(r); // 返回第一次出现 "ain" 的地方。 
} 


本示例说明带 g 标志设置的js中match函数方法的用法 

function MatchDemo(){ 
var r, re; // 声明变量。 
var s = "The rain in Spain falls mainly in the plain"; 
re = /ain/ig; // 创建正则表达式模式。 
r = s.match(re); // 尝试去匹配搜索字符串。 
return(r); // 返回的数组包含了所有 "ain" 
// 出现的四个匹配。 
} 


下面几行代码演示了字符串文字的js中match函数方法的用法。 
var r, re = "Spain"; 
r = "The rain in Spain".replace(re, "Canada"); 

match()方法用于从字符串中查找指定的值,本方法类似于indexOf()和lastindexOf(),不同的是它返回的是指定的值,而 不是指定值在字符串中的位置。indexOf()和lastindexOf()方法返回位置数字 如果找不到返回-1。注意区分大小写 
 

<script type="text/javascript"> 
var str="Hello world!" 
document.write(str.match("world") + "") 
document.write(str.match("World") + "") 
document.write(str.match("worlld") + "") 
document.write(str.match("world!")) 
</script>

输出结果为

worldnullnullworld!    第一个和第四个能匹配到,返回匹配的值,第二个大小写不匹配,第三个多了个l,返回null

原网址:https://www.cnblogs.com/jingaier/p/6709653.html

js正则标志/g,/i,/m说明和实例

一,js正则标志/g,/i,/m说明

1,/g 表示该表达式将用来在输入字符串中查找所有可能的匹配,返回的结果可以是多个。如果不加/g最多只会匹配一个

2,/i  表示匹配的时候不区分大小写

3,/m 表示多行匹配,什么是多行匹配呢?就是匹配换行符两端的潜在匹配。影响正则中的^$符号

二,实例说明

1,/g的用法

<script type="text/javascript">  
    str = "tankZHang (231144)"+  
     "tank ying (155445)";  
    res = str.match(/tank/);    //没有加/g  
    alert(res);                 //显示一个tank  
  
    res = str.match(/tank/g);   //加了/g  
    alert(res);                 //显示为tank,tank  
</script>

2,/i的用法

<script type="text/javascript">
    str = "tankZHang (231144)" +
     "tank ying (155445)";
    res = str.match(/zhang/);
    alert(res);                  //显示为null  

    res = str.match(/zhang/i);   //加了/i  
    alert(res);                  //显示为ZHang  
</script>

3,/m的用法

<script type="text/javascript">
    var p = /$/mg;
    var s = '1\n2\n3\n4\n5\n6';
    alert(p.test(s));  //显示为true  
    alert(RegExp.rightContext.replace(/\x0A/g, '\\a'));  //显示\a2\a3\a4\a5\a6  
    alert(RegExp.leftContext);    //显示为竖的2345  
    alert(RegExp.rightContext);   //显示为6  

    var p = /$/g;
    var s = '1\n2\n3\n4\n5\n6';
    alert(p.test(s));  //显示为true  
    alert(RegExp.rightContext.replace(/\x0A/g, '\\a'));  //什么都不显示  
    alert(RegExp.leftContext);    //显示为竖的123456  
    alert(RegExp.rightContext);   //什么都不显示  

    var p = /^/mg;
    var s = '1\n2\n3\n4\n5\n6';
    alert(p.test(s));    //显示为true  
    alert(RegExp.rightContext.replace(/\x0A/g, '\\a')); //显示为1\a2\a3\a4\a5\a6  
    alert(RegExp.leftContext);     //显示为竖的12345  
    alert(RegExp.rightContext);    //显示为6  
</script>   
//从上例中可以看出/m影响的^$的分割方式  
上面说的三个例子,/i,/g,/m分开来说的,可以排列组合使用的。个人觉得/m没有多大用处  

原网址:https://www.cnblogs.com/william-lin/p/3480231.html

猜你喜欢

转载自blog.csdn.net/qq_33380252/article/details/84030881