String.prototype.match()
(aka: the match method on strings) can allow you to switch out strings or set conditions if a string or any data is matched. It then stores that data in a new array.
String.prototype.match()
(又名:字符串的match方法)可以让您在匹配字符串或任何数据时切换出字符串或设置条件。 然后,它将数据存储在新数组中。
First the syntax and then the explanation:
首先是语法,然后是解释:
let newArray = string.match(condition);
术语 (Terminology)
The string match()
method will return an array with the items being matches from a provided regular expression
found in the string. You can read more about regular expressions in JavaScript here.
字符串match()
方法将返回一个数组,其中的项与字符串中提供的regular expression
的项匹配。 您可以在此处阅读有关JavaScript中正则表达式的更多信息 。
Remember, when all conditions are matched, those results will be stored in a new array.
请记住,当所有条件都匹配时,这些结果将存储在新数组中 。
Take the following example:
请看以下示例:
const intro = "Hello Alligators, Hello Devs, how are you?"
const regex = /Hello/g;
const greeting = intro.match(regex);
The above will give us an array like this: ["Hello", "Hello"]
. This works fine if we know the exact string we’re looking to match, but what about dynamic content or an actual use case?
上面的代码将给我们这样的数组: ["Hello", "Hello"]
。 如果我们知道要查找的确切字符串,则此方法很好,但是动态内容或实际用例呢?
Here’s a simple example that finds repeated letters in a string:
这是一个简单的示例,可在字符串中查找重复的字母:
const str = 'See you later, Alligator! Not so soon baboon!';
const matches = str.match(/([a-z])\1+/gi);
console.log('H' + matches.join(""));
// "Heelloooo"
Though these are simple examples, the deeper you learn about regular expressions, the more powerful this string method becomes. The simple use of i
for insensitive case allows the match method to highlight more entries.
尽管这些都是简单的示例,但是您对正则表达式的了解越深,这种字符串方法就越强大。 不区分大小写的i
的简单用法允许match方法突出显示更多条目。
The match method has 3 modes…
匹配方法有3种模式…
1st: If the
g
(global) flag is used for your RegEx, you’ll get all results stored in an array.1st :如果将
g
(全局)标志用于RegEx,则将所有结果存储在数组中。2nd: If there are no
g
flag used, the first match will return an array with keys/values sharing index of the first matched expression, the full input and then the capturing groups. In other words, the same result as with usingRegExp.exec()
.2nd :如果没有使用
g
标志,则第一个匹配项将返回一个数组,该数组包含第一个匹配表达式的键/值共享索引,完整输入以及捕获组。 换句话说,与使用RegExp.exec()
结果相同。
let newYear = "Happy New Year";
let results = newYear.match(/new/i);
// [ 'New', index: 6, input: 'Happy New Year', groups: undefined ]
3rd: If there’s no match, the method returns
null
, or, with the following code, an empty array:3rd :如果不匹配,则该方法返回
null
,或者使用以下代码返回一个空数组:
let results = newYear.match(regex) || [];
结论 (Conclusion)
Match is a fun little method that can be used in a lot of creative ways like pulling out keywords from a paragraph or replacing words if the condition matches the regex. Take the time to learn about Regular Expressions in JavaScript. It’ll make match even more useful for you.
匹配是一种有趣的小方法,可用于许多创造性方式,例如从段落中提取关键字或在条件与正则表达式匹配时替换单词。 花时间学习JavaScript中的正则表达式 。 它会使匹配对您更有用。
翻译自: https://www.digitalocean.com/community/tutorials/js-string-match