迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 javascript 中检查一个字符串是否与 REGEX 匹配

作者:迹忆客 最近更新:2023/03/09 浏览次数:

JavaScript 有一种不同的方式来表示正则表达式修饰符、量词和元字符。但这些通常表示与任何其他正则表达式约定相同的含义。

匹配字符串甚至在句子中搜索字符串或字符时会计算正则表达式匹配。从技术上讲,我们从给定字符串中提取子字符串并与正则表达式进行比较。

在这里,我们将讨论通过生成的正则表达式检索匹配项的三种方法。在这种情况下,我们将使用 test()match()exec()matchAll() 方法。

关于 test() 方法,首先要了解的是格式推断匹配条件。这种特定的方法只喜欢布尔格式来回答。

方便在有条件的情况下进一步操作功能。所以,一般形式是 regular_expression.test(string)

让我们跳到代码上看个清楚。

代码片段:

var string = 'abc123 is a ridiculous name';
var regularExpression = /\w\d+/;
var found = regularExpression.test(string);
console.log(found);

输出:

使用 test() 方法与正则表达式进行字符串匹配

如你所见,这里的正则表达式描述了查找单个单词后跟至少一个或多个数字。根据我们的字符串,它有一个匹配,因此结果是 true

用正则表达式匹配确切的字符串需要显示匹配。当我们依赖 match() 方法时,它返回整个字符串的正确匹配。

它将利用正则表达式中的每个表达式,最终输出将是匹配的字符串。

代码片段:

var string = 'abc123 is a ridiculous name';
var regex = /i\w/g;
var found = string.match(regex);
console.log(found);

输出:

使用 match() 方法与正则表达式进行字符串匹配

在这种情况下,我们有格式 string.match(regex),这里正则表达式要求查找具有 i 后跟任何其他字符词的子集。所以它成功地做到了,但在下一节中,我们还将看到 match()exec() 方法有何不同。

在前面的 match() 方法示例中,我们可视化了正则表达式 /i\w/g 如何提取所有可能的匹配项。但是在这里,对于 exec() 方法,答案将只有一个字符串的初始匹配。

这就是 exec() 方法所做的事情,这很方便,并且通常作为约束工作。让我们看一下代码以便更好地理解。

代码片段:

var string = 'abc123 is a ridiculous name';
var regex = /i\w/g;
var found = regex.exec(string);
console.log(found);

输出:

使用 exec() 方法与正则表达式进行字符串匹配

exec() 方法无法从字符串中定义所有可能的匹配项。因此,更新包括 matchAll() 方法以返回与数组格式的正则表达式匹配的所有子集。

代码片段:

var string = 'abc123 is a ridiculous name';
var regex = /i\w/g;
var found = string.matchAll(regex);
for (var match of found){
    console.log(match);
}

输出:

使用 matchAll() 方法与正则表达式进行字符串匹配

exec()matchAll() 方法之间的核心区别在于 exec() 方法仅返回第一个匹配项,并且当其后跟一个 while 循环时,它可以遍历所有可能的匹配项。

但是在 matchAll() 方法的情况下,如果不启动循环系统,你将无法访问匹配项。虽然它确实会导致可能的匹配。

这个约定也符合 for...of 循环。总的来说,match() 方法满足所有类型的查找字符串匹配的查询。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便