JavaScript字符串匹配方法快速指南

news/2024/7/7 10:24:12

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 using RegExp.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


http://www.niftyadmin.cn/n/3648912.html

相关文章

软件测试管理--前言

国内IT公司的软件产品质量较差已经是一个不争的事实。测试作为质量管理的一个方面,更是重灾区。目前国内市场上有关测试方面的书籍基本都是翻译的,因此很多都不符合我国软件管理的发展现状。国内的测试工程师大多是没有开发和测试经验的新手,…

Android 应用程序升级到 5.0 需要注意的问题

Android 5.0,代号 Lollipop,源码终于在2014年12月3日放出,国内一大批厂商跟进。最大的改变是默认使用 ART(Android Runtime) ,替换了之前的 Dalvik 虚拟机,提出了 Material Design 界面风格。之前发布的 app 可能需要作…

[dotNET]COM组件转换成.NET组件{ZT}

原作者:http://www.cnblogs.com/wubn/archive/2004/09/29/47997.aspx利用类型库导入器(Type Library Importer)可以将其包装成一个.NET组件,然后就可以像使用.NET组件一样使用它了。 .NET框架只是提供了一个包装,并没有真正改变原有的对象1.找…

Java中必须了解的常用类

包装类 int、float、double、boolean、char等都不具备对象的特性,例如基本方法不能调用,功能简单等,为了让基本数据类型具备对象的特性,java为每个数据提供了一个包装类,这样我们就可以操作对象那样操作数据了。 J…

如何在Ubuntu 18.04上安装和使用Radamsa来模糊测试程序和网络服务

The author selected the Electronic Frontier Foundation Inc to receive a donation as part of the Write for DOnations program. 作者选择Electronic Frontier Foundation Inc接受捐赠,作为Write for DOnations计划的一部分。 介绍 (Introduction) Security t…

真的不是半成品?安卓5.0已知问题汇总

1安卓5.0的Bug(一)安卓5.0已经正式向消费者推送,这是一个革命性的安卓版本,带来了全新的用户界面以及更高的系统运行效率。然而,巨大的变革也带来了巨量问题,从安卓L预览版,到现在的安卓5.0正式…

[dotNET]曾毅翻译的《体验STL.NET》

体验STL.NET[翻译] zengyi820 2004-10-07 为了更好的使STL适合.NET开发,Visual C产品组,在2005版的Visual C中重新设计了STL,并命名为STL.NET,从Beta1版本的产品中开始提供。在STL.NET的设计中,STL的实现使用了CLI泛型和C模版机制…

什么时候应该使用CSS!important规则?

The !important rule in CSS gives you the ability to override styles, but is this power too much for a lone software developer? Read this article to find out what it is, and when it’s a good idea to use it! CSS中的!important规则使您能够覆盖样式&#xff0c…