js的正则表达式
1. 语法
1.1. 直接量语法
/pattern/attributes
1.2. 创建 RegExp 语法
new RegExp(pattern, attributes);
参数:参数 pattern 是一个字符串,指定正则表达式的匹配模式,attributes 为一个修饰符,i、m、g 三种选项,分别表示不区分大小写、多行匹配、全局匹配 返回值:一个 RegExp 对象,具有指定的模式和标志
2. RegExp对象方法
2.1. test 该方法用于检测一个字符串是否匹配某个模式。
RegExp.test(str) 参数:str 为需要检测的字符串 返回值:满足正则表达式返回 true,否则返回 false 通过 while(RegExp.test(str)) {} 可以得到每个匹配到的位置
const reg = new RegExp('test', 'g');
reg.lastIndex = 0;
while (reg.test('testtesttesttest')) {
console.log(reg.lastIndex); // 输出次匹配到的字符串的末位置
}
2
3
4
5
2.2. exec 该方法用于检索字符串中的正则表达式的匹配
RegExp.exec(str) 参数:str 是需要检测的字符串 返回值:返回一个数组,存放匹配的结果,如果未找到匹配,则返回值为 null
var str = "javascript html css";
console.log(/html/.exec(str)); // ["html", index: 11, input: "javascript html css"]
返回的数组的第一个元素是与正则表达式相匹配的文本,该方法还返回2个属性,index属性声明的是匹配文本的第一个字符的位置;input属性则存放的是被检索的字符串string。
// 假如没有找到的话,则返回null
console.log(/node/.exec(str)); // null
2
3
4
5
3. String 字符串对象支持正则表达式方法
3.1. search 该方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的字符串
str.search(regexp); 参数:regexp 可以是在 stringObject 中检索的字符串,也可以是需要检索的 RegExp 对象 返回值:stringObject 中第一个与 regexp 对象相匹配的子串的起始位置。如果没有找到任何匹配的子串,则返回 - 1; 该方法不支持全局匹配
var str = "hello world,hello world";
// 返回匹配到的第一个位置
console.log(str.search(/hello/)); // 0
// search方法不执行全局匹配,它将忽略标志g,同时它也没有regexp对象的lastIndex的属性,且总是从字符串开始位置进行查找,总是返回的是stringObject匹配的第一个位置。
console.log(str.search(/hello/g)); //0
console.log(str.search(/world/)); // 6
// 也可以是检索字符串中的字符
console.log(str.search("wo")); // 6
// 如果没有检索到的话,则返回-1
console.log(str.search(/longen/)); // -1
// 我们检索的时候 可以忽略大小写来检索
var str2 = "Hello";
console.log(str2.search(/hello/i)); // 0
2
3
4
5
6
7
8
9
10
11
12
13
3.2. match 该方法用于在字符串内检索指定的值,或找到一个或者多个正则表达式的匹配。该方法类似于 indexOf() 或者 lastIndexOf(); 但是它返回的是指定的值,而不是字符串的位置。
str.match(regexp); 参数:regexp 可以是在 stringObject 中检索的字符串,也可以是需要检索的 RegExp 对象 返回值:存放匹配成功的数组;如果没有找到任何的一个匹配,那么它将返回的是 null 全局匹配时返回所有的匹配到的字符串的数组,非全局匹配时为第一个匹配到的位置的详细信息
var str = "hello world";
// 返回的数组内有三个元素,第一个元素的存放的是匹配的文本,还有二个对象属性;index属性表明的是匹配文本的起始字符在stringObject中的位置;input属性声明的是对stringObject对象的引用;
console.log(str.match("hello")); // ["hello", index: 0, input: "hello world"]
console.log(str.match("Hello")); // null
console.log(str.match(/hello/)); // ["hello", index: 0, input: "hello world"]
// 全局匹配也返回一个数组,但有些不一样;它的数组元素中存放的是 stringObject 中所有的匹配子串,而且也没有 index 属性或 input 属性。
var str2="1 plus 2 equal 3"
console.log(str2.match(/\d+/g)); //["1", "2", "3"]
2
3
4
5
6
7
8
3.3 split 该方法把一个字符串分割成字符串数组
语法:str.split(separator,howmany) 参数: 1.separator[必填项],字符串或正则表达式,该参数指定的地方分割 stringObject; 2.howmany[可选] 该参数指定返回的数组的最大长度,如果设置了该参数,返回的子字符串不会多于这个参数指定的数组。如果没有设置该参数的话,整个字符串都会被分割,不考虑他的长度。 返回值:一个字符串数组。该数组通过在 separator 指定的边界处将字符串 stringObject 分割成子字符串。
var str="How are you doing today?"
console.log(str.split(" ")) //["How", "are", "you", "doing", "today?"]
console.log(str.split("")) //["H", "o", "w", " ", "a", "r", "e", " ", "y", "o", "u", " ", "d", "o", "i", "n", "g", " ", "t", "o", "d", "a", "y", "?"]
console.log(str.split(" ",3)) //["How", "are", "you"]
// 也可以使用正则表达式
console.log(str.split(/\s+/)) //["How", "are", "you", "doing", "today?"]
2
3
4
5
6
3.4 replace 该方法用于在字符串中使用一些字符替换另一些字符,或者替换一个与正则表达式匹配的子字符串。
语法:str.replace(regexp/substr,replacement); 参数:regexp/substr 可以是字符串或者是需要替换模式的 RegExp 对象;replacement 可以是替换的文本或者是生成替换文本的函数。 返回值:返回替换后的新字符串。 字符串 stringObject 的 replace() 方法执行的是查找并替换的操作。它将在 stringObject 中查找与 regexp 相匹配的子字符串,然后用 replacement 来替换这些子串。如果 regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。replacement 可以是字符串,也可以是函数。如果它是字符串,那么每个匹配都将由字符串替换。但是 replacement 中的 $ 字符具有特定的含义。如下表所示,它说明从模式匹配得到的字符串将用于替换。
var str = "hello world";
// 使用字符串替换字符串
var s1 = str.replace("hello","a");
console.log(s1);// a world
// 使用正则替换字符串
var s2 = str.replace(/hello/,"b");
console.log(s2); // b world
// 使用正则全局替换字符串
var s3 = str.replace(/l/g,'');
console.log(s3); // heo word
// $1,$2 代表的是第一个和第二个子表达式相匹配的文本
// 子表达式需要使用小括号括起来,代表的含义是分组
var name = "longen ,yunxi";
var s4 = name.replace(/(\w+)\s*,\s*(\w+)/,"$2 $1");
console.log(s4); // "yunxi,longen"
console.log('hello world'.replace(/w/g, '$&')) //hello world
console.log('hello world'.replace(/w/g, '$$')) //hello $orld
console.log('hello world'.replace(/w/g, '$`')) //hello hello orld
console.log('hello world'.replace(/w/g, "$'")) //hello orldorld
// replace 第二个参数也可以是一个function 函数
// 单词首字母大写
var name = 'aaa bbb ccc';
var uw=name.replace(/\b\w+\b/g, function(word){
return word.substring(0,1).toUpperCase()+word.substring(1);}
);
console.log(uw) //Aaa Bbb Ccc
var name2 = "123cbc45678rtyu909876pjkl54321";
name2.replace(/\d+/g,function(v){
console.log(v);
/*
* 第一次打印123
* 第二次打印45678
* 第三次打印909876
* 第四次打印54321
*/
});
/*
* 如下函数,回调函数参数一共有四个
* 第一个参数的含义是 匹配的字符串
* 第二个参数的含义是 正则表达式分组内容,没有分组的话,就没有该参数,
* 如果没有该参数的话那么第四个参数就是undefined
* 第三个参数的含义是 匹配项在字符串中的索引index
* 第四个参数的含义是 原字符串
*/
name2.replace(/(\d+)/g,function(a,b,c,d){
console.log(a);
console.log(b);
console.log(c);
console.log(d);
/*
* 如上会执行四次,值分别如下(正则使用小括号,代表分组):
* 第一次: 123,123,0,123cbc45678rtyu909876pjkl54321
* 第二次: 45678,45678,6,123cbc45678rtyu909876pjkl54321
* 第三次: 909876,909876,15,123cbc45678rtyu909876pjkl54321
* 第四次: 54321,54321,25,123cbc45678rtyu909876pjkl54321
*/
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57