首页 > 编程语言 >JavaScript中String的match方法详解

JavaScript中String的match方法详解

时间:2022-11-25 20:55:36浏览次数:38  
标签:返回 Infinity String 正则表达式 JavaScript var prototype match

 


 

String.prototype.match()

** String.prototype.match()方法返回通过一个正则表达式匹配到的字符串结果。**

  1. var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
  2. var regex = /[A-Z]/g;
  3. var found = paragraph.match(regex);
  4. console.log(found);
  5. // 输出: Array ["T", "I"]

语法

str.match(regexp)

参数

regexp
一个正则表达式对象。如果传递了一个非正则表达式对象,函数会执行new RegExp(obj)将他转换成一个字符串对象。如果没有传递任何参数,函数将返回空字符串数组:[“”]。

返回值

如果有匹配值,String.prototype.match()返回一个数组,这个数组的内容取决于正则表达式参数中是否有g修饰符。如果没有匹配值,返回null。
(1)如果正则表达式中有g修饰符。所有匹配到的值会按照数组格式被返回,不会返回捕获组。
(2)如果没有g修饰符。只有第一个匹配到的值会被返回,同时还会返回第一个匹配到的值内部的所有捕获组。在这种情况下,返回值会多包含以下几个属性:

  • groups:如果匹配值中包含捕获组,返回值中将包含捕获组信息的各项值。否则为undefined。
  • index:匹配值在整个字符串中的索引。
  • input:整个字符串。

描述

如果String.prototype.match()参数的正则表达式中没有包含g修饰符,str.match()将会和RegExp.prototype.exec()方法返回一样的结果。

  • 如果只想知道一个字符串是否匹配一个正则表达式,使用RegExp.prototype.test()。
  • 如果想使用一个包含g修饰符的正则表达式匹配中获得捕获组,使用RegExp. prototype.exec()。

示例

直接使用String.prototype.match()

下面的例子中,String.prototype.match()将会查找一个以”Chapter”开头的,其后跟随一个或多个数字字符,数字字符后跟随另个或多个’.+数字字符’。在下面代码中正则表达式中有i修饰符,所以整个正则表达式忽略大小写。

  1. var str = 'For more information, see Chapter 3.4.5.1';
  2. var re = /see (chapter \d+(\.\d)*)/i;
  3. var found = str.match(re);
  4. console.log(found);
  5. // 输出 [ 'see Chapter 3.4.5.1', 'Chapter 3.4.5.1', '.1',
  6. // index: 22,
  7. // input: 'For more information, see Chapter 3.4.5.1' ]
  8. // 'see Chapter 3.4.5.1'是整个匹配项值.
  9. // 'Chapter 3.4.5.1' 被捕获组'(chapter \d+(\.\d)*)'捕获.
  10. // '.1' 被最后一个捕获组'(\.\d)捕获'.
  11. // 'index'属性(值22)是第一个匹配字符在整个字符串中的索引位置.
  12. // 'input'是整个字符串.

在String.prototype.match()中使用g修饰符

下面的例子中,参数正则表达式中包含g修饰符,结果将会返回所有被匹配的值。

  1. var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  2. var regexp = /[A-E]/gi;
  3. var matches_array = str.match(regexp);
  4. console.log(matches_array);
  5. // ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']

在String.prototype.match()中不传入参数

  1. var str = "Nothing will come of nothing.";
  2. str.match(); // 返回 [""]

一个非正则表达式作为参数

当一个参数是一个字符串或一个数字,函数内部将会用new RegExp(obj)将他转换成正则表达式对象。如果参数是一个包含“+”符号的正数,则RegExp()函数将会忽略这个“+”符号。

  1. var str1 = "NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.",
  2. str2 = "My grandfather is 65 years old and My grandmother is 63 years old.",
  3. str3 = "The contract was declared null and void.";
  4. str1.match("number"); // "number" 是个字符串,返回["number"]
  5. str1.match(NaN); // NaN 的数据类型是数字,返回 ["NaN"]
  6. str1.match(Infinity); // Infinity 的数据类型是数字,返回 ["Infinity"]
  7. str1.match(+Infinity); // 返回 ["Infinity"]
  8. str1.match(-Infinity); // 返回 ["-Infinity"]
  9. str2.match(65); // 返回 ["65"]
  10. str2.match(+65); // “+”符号将会被忽略,返回 ["65"]
  11. str3.match(null); // 返回 ["null"]

标签:返回,Infinity,String,正则表达式,JavaScript,var,prototype,match
From: https://www.cnblogs.com/onesea/p/16926321.html

相关文章