当前位置:   article > 正文

JavaScript 正则表达式的 5 个方法_js 正则表达式的方法

js 正则表达式的方法

介绍一些 JavaScript 中的编写正则表达式的常见用法。

1. match()
match() 与字符串一起使用以检查字符串和正则表达式 regex 之间的匹配,以正则表达式为参数。

语法:

str.match(regex);
方法返回 3 个可能的值:

如果正则表达式包含一个 g 标记,即为全局匹配,它将返回一个包含所有匹配项的数组,没捕获组信息;
如果正则表达式没有 g 标记,它将返回一个包含第一个匹配项和其相关的捕获组的数组;
如果根本没有匹配项,则返回 null 。
groups:一个命名捕获组的对象,其键是名称,值为捕获组,如果未定义命名捕获组,则为 undefined。
带有标记 g 的实例代码:

  1. const strText = "Hello China";
  2. const regex = /[A-Z]/g; // 大写字母正则表达式
  3. console.log(strText.match(regex)); // [ 'H', 'C' ]


没有标记 g 的实例代码:

  1. const text = 'Hello World';
  2. const regex = /[A-Z]/; //Capital letters regex.
  3. console.log(text.match(regex)); // [ 'H', index: 0, input: 'Hello China', groups: undefined ]


当没有匹配的实例代码:

  1. const strText = "hello china";
  2. const regex = /[A-Z]/; // 大写字母正则表达式
  3. console.log(strText.match(regex)); // null


2. test()
test() 用于测试指定字符串和正则表达式之间是否匹配,接受一个字符串作为其参数,并根据是否匹配返回 true 或 false 。

假设在下面的字符串 strText 中检测单词 china 是否存在。可以为查找单词创建一个正则表达式并测试该正则表达式和字符串 strText 之间是否匹配。

  1. const strText = "hello china";
  2. const regex = /china/;
  3. console.log(regex.test(strText)); // true


下面是没有匹配的实例代码:

  1. const strText = "hello China";
  2. const regex = /china/;
  3. console.log(regex.test(strText)); // false


从上面代码可以看到,大小写是会影响匹配的结果,如果需要忽略大小写,则需要使用标记 i,如下代码:

  1. const strText = "hello China";
  2. const regex = /china/i;
  3. console.log(regex.test(strText)); // true


请注意,在 语法上 .match() 与 .test() 在使用上是 “相反” 的
3. search()
search() 方法是一个字符串方法,可以将其与正则表达式一起使用。可以将正则表达式作为参数传递给它,以在字符串中搜索匹配项。

方法返回第一个匹配项在整个字符串中的位置(索引),如果没有匹配项,则返回 -1。

匹配的实例:

  1. const strText = "hello china,i love china";
  2. const regex = /china/;
  3. console.log(strText.search(regex)); // 6


没有匹配的实例:

  1. const strText = "hello china,i love china";
  2. const regex = /devpoint/;
  3. console.log(strText.search(regex)); // -1


4. replace()
replace() 是在字符串中搜索指定的值或正则表达式并将其替换为另一个值,方法接受两个参数:

要搜索的值
要替换的新值

方法返回一个包含被替换后的新字符串,需要注意的是,它不会改变原始字符串,并且只会替换搜索到的第一个值。

实例代码:

  1. const strText = "hello world,i love world";
  2. const regex = /world/;
  3. console.log(strText.replace(regex, "china")); // hello china,i love world

5. replaceAll()
replaceAll() 类似于方法 replace() ,但它允许替换字符串中所有匹配的值或正则表达式。

它接受两个参数:

要搜索的值,如果是正则,则必须带上全局标记 g
要替换的新值
它返回一个包含所有新值的新字符串,同样也不会更改原始字符串。

实例代码:

  1. const strText = "hello world,i love world";
  2. const regex = /world/g;
  3. console.log(strText.replaceAll(regex, "china")); // hello china,i love china


等效于如下代码:

  1. const strText = "hello world,i love world";
  2. console.log(strText.replaceAll("world", "china")); // hello china,i love china


通过正则查找替换,在正则表达式中加上全局标记 g , 同样可以替换所有符合正则条件的字符串,如下代码:

  1. const strText = "hello world,i love world";
  2. const regex = /world/g;
  3. console.log(strText.replace(regex, "china")); // hello china,i love china


————————————————
版权声明:本文为CSDN博主「高先生的猫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/z591102/article/details/120011104

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读