迹忆客 专注技术分享

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

在 JavaScript 中检查字符串是否包含子字符串

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

本教程讲解如何在 JavaScript 中检查字符串是否包含子字符串。

这是一个简单而常见的任务,但是不同的语言以不同的方式处理它。我们将看到 JavaScript 中的内置方法和自定义编写的方法来实现上述目标。

它是检查子字符串是否存在的最强大,最传统的方法之一。与内置方法相比,它为我们提供了很大的灵活性,因为使用 JavaScript 内置方法,我们只能检查常量字符串,而不能更改其行为的区分大小写。我们将使用 RegExp.test() 方法检查字符串中的子字符串。

let str = 'delftstack';
console.log(/stack/.test(str));

输出:

true

这里我们使用 .test() 函数来查看字符串 delftstack 中是否存在子字符串 stack 并得到 true 的结果。

但是正则表达式的优点在于我们可以执行不区分大小写的搜索,而内置的 JavaScript 方法是不可能做到的,我们甚至可以在不知道它是什么的情况下寻找固定长度的数字子字符串这样的模式。

let str = 'delftstack';
console.log(stack/i.test(str));

输出:

true

我们将 i 附加到表达式/stack/上,然后可以进行不区分大小写的搜索。

ES6 引入了 .includes() 函数,该函数可帮助我们检查字符串中是否存在给定的子字符串。它返回一个布尔值,以 truefalse 表示存在。这是区分大小写的方法,因此,如果子字符串 Ss 匹配,它将返回 false

此方法有 2 个参数:第一个是子字符串,第二个是我们要从其开始在字符串内搜索的索引。如果第二个参数在左边界之外,即小于 0,则 JavaScript 从索引 0 开始搜索。如果它大于字符串的长度,则直接返回 false。

const str = "delftstack";
const substr = "stack";
console.log(str.includes(substr));

输出:

true

在上面的代码中,我们将子字符串传递给函数,并且由于字符串 delftstack 中存在 stack,因此它返回 true

const str = "delftstack";
const substr = "stack";
console.log(str.includes(substr,4));

输出:

true

该代码从第五索引开始搜索 stack 一词。

在 ES6 引入 String.includes() 方法之前,唯一的内置方法是 .indexOf()。它与 String.includes() 方法非常相似。如果子字符串存在于给定的字符串内,则此方法将返回子字符串的第一个字符的索引,而不是返回布尔值;如果子字符串不存在,则返回 -1

const str = "delftstack";
const substr = "stack";
console.log(str.indexOf(substr)!=-1); 

输出:

true

它可能很旧,但是更精确,因为它可以像 .includes() 一样使用,只需在函数的结果上添加检查以查看其是否等于 -1 即可。与 .includes() 不同,它向我们返回了子字符串开头的确切索引,并且可以有更多的实际意义。

所有主流浏览器均支持除 String.includes() 之外的所有上述方法。String.includes() 由 ES6 引入,但 Internet Explorer 仍不支持。实现此目的的另一种方法是使用 KMP(Knuth-Morris-Pratt)算法,这是一种具有广泛应用的 O(n) 算法。它本身就是一个巨大的话题,我们将在以后的文章中讨论。

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便