全面解析JS字符串和正则表达式中的match、replace、exec等函数

网络编程 发布日期:2024/10/4 浏览次数:1

正在浏览:全面解析JS字符串和正则表达式中的match、replace、exec等函数

正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。

正则表达式由于不经常使用,所以容易经常忘记,下面小编把常用的函数和功能,简明扼要的罗列在此,以备日后查看:

RegExp对象的函数常用的有2个

1、test函数

用法:RegExpObject.test(string)

返回:如果字符串 string 中含有与 RegExpObject 匹配的文本,则返回 true,否则返回 false。

描述:这个方法没有什么特殊之处,对修饰符g没有什么特殊处理

示例:

var url = 'http://www.baidu.com"htmlcode">
var url = 'http://www.baidu.com"a=1", "a", "1", index: 21, input: "http://www.baidu.com"]
console.log(reg.exec(url)); //["b=2", "b", "2", index: 25, input: "http://www.baidu.com"]
console.log(reg.exec(url)); //["c=3", "c", "3", index: 29, input: "http://www.baidu.com"]
console.log(reg.exec(url)); //null
reg.lastIndex = 0; //这段代码很重要哦,注意理解
console.log(reg.exec(url)); //["a=1", "a", "1", index: 21, input: http://www.baidu.com"htmlcode">
var url = 'http://www.baidu.com"a=1", "a", "1", index: 21, input: "http://www.baidu.com"]
console.log(reg.exec(url)); //["a=1", "a", "1", index: 21, input: "http://www.baidu.com"]
console.log(reg.exec(url)); //["a=1", "a", "1", index: 21, input: "http://www.baidu.com"]
console.log(reg.exec(url)); //["a=1", "a", "1", index: 21, input: "http://www.baidu.com"]
reg.lastIndex = 0; 
console.log(reg.exec(url)); //["a=1", "a", "1", index: 21, input: http://www.baidu.com"color: #ff0000">String对象的函数支持正则的有4个,我们只说其中的2个

1、match函数

用法:stringObject.match(searchvalue | regexp),这里我们只说regexp模式

返回值:存放匹配结果的数组。该数组的内容依赖于 regexp 是否具有全局标志 g。

描述:

match() 方法将检索字符串 stringObject,以找到一个或多个与 regexp 匹配的文本。这个方法的行为在很大程度上有赖于 regexp 是否具有标志 g。

如果 regexp 没有标志 g,那么 match() 方法就只能在 stringObject 中执行一次匹配。如果没有找到任何匹配的文本, match() 将返回 null。否则,它将返回一个数组,其中存放了与它找到的匹配文本有关的信息。该数组的第 0 个元素存放的是匹配文本,而其余的元素存放的是与正则表达式的子表达式匹配的文本。除了这些常规的数组元素之外,返回的数组还含有两个对象属性。 index 属性声明的是匹配文本的起始字符在 stringObject 中的位置,input 属性声明的是对 stringObject 的引用。

如果 regexp 具有标志 g,则 match() 方法将执行全局检索,找到 stringObject 中的所有匹配子字符串。若没有找到任何匹配的子串,则返回 null。如果找到了一个或多个匹配子串,则返回一个数组。不过全局匹配返回的数组的内容与前者大不相同,它的数组元素中存放的是 stringObject 中所有的匹配子串,而且也没有 index 属性或 input 属性。

示例:

不带修饰符g

var url = 'http://www.baidu.com"a=1", "a", "1", index: 21, input: "http://www.baidu.com"]
console.log(result.index); //21
console.log(result.input); //http://www.baidu.com"htmlcode">
var url = 'http://www.baidu.com"a=1", "b=2", "c=3"]
console.log(result.index); //undefined
console.log(result.input); //undefined 

发现不一样的地方了吗,把函数描述好好读一遍,就明白了^_^

2、replace函数

用法:stringObject.replace(regexp/substr,replacement)

返回值:一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。

描述:字符串 stringObject 的 replace() 方法执行的是查找并替换的操作。它将在 stringObject 中查找与 regexp 相匹配的子字符串,然后用 replacement 来替换这些子串。如果 regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。

replacement 可以是字符串,也可以是函数。如果它是字符串,那么每个匹配都将由字符串替换。但是 replacement 中的 $ 字符具有特定的含义。如下表所示,它说明从模式匹配得到的字符串将用于替换。

字符 替换文本 $1、$2、...、$99 与 regexp 中的第 1 到第 99 个子表达式相匹配的文本。 $& 与 regexp 相匹配的子串。 $` 位于匹配子串左侧的文本。 $' 位于匹配子串右侧的文本。 $$ 直接量符号。(意思就是要替换为 $ 符号的时候,就写两个$)

示例:

不带修饰符g

var url = 'http://www.baidu.com"htmlcode">
var url = 'http://www.baidu.com"htmlcode">
var url = 'http://www.baidu.com"$&")
console.log(url1); //http://www.baidu.com"$1")
console.log(url1); //http://www.baidu.com"$2")
console.log(url1); //http://www.baidu.com"$'")
console.log(url1); //http://www.baidu.com"$&")
console.log(url1); //http://www.baidu.com"$1")
console.log(url1); //http://www.baidu.com"$2")
console.log(url1); //http://www.baidu.com"$'")
console.log(url1); //http://www.baidu.com?&b=2&c=3&&c=3&

以上所述是小编给大家介绍的全面解析JS字符串和正则表达式中的match、replace、exec等函数,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!