迹忆客 专注技术分享

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

JavaScript 中检查字符串是否以指定的子字符串开头

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

要检查字符串是否以子字符串开头,请对字符串调用 startsWith() 方法,并将子字符串作为参数传递给它。 如果字符串以子字符串开头,则 startsWith 方法返回 true,否则返回 false

const str = 'hello world';

if (str.startsWith('hello')) {
  // 👇️ this runs
  console.log('✅ string starts with hello');
} else {
  console.log('⛔️ string does NOT start with hello');
}

我们使用 String.startsWith 方法来确定字符串是否以特定子字符串开头。

该方法返回一个布尔结果:

  • 如果字符串以子字符串开头,则为真
  • 如果没有则为假

startsWith 方法执行区分大小写的搜索,如果要忽略大小写,请在进行比较之前将字符串和子字符串转换为小写。

const str = 'HELLO world';
const substr = 'hello';

if (str.toLowerCase().startsWith(substr.toLowerCase())) {
  // 👇️ this runs
  console.log('✅ string starts with hello');
} else {
  console.log('⛔️ string does NOT start with hello');
}

Internet Explorer 不支持 startsWith 方法。 如果需要支持浏览器,请使用本文介绍的下一种方法。

使用 indexOf 检查 String 是否以 Substring 开头

要检查字符串是否以子字符串开头,请对字符串调用 indexOf() 方法,将子字符串作为参数传递给它。 如果 indexOf 方法返回 0,则字符串以子字符串开始,否则不是。

const str = 'hello world';
const substr = 'hello';

if (str.indexOf('hello') === 0) {
  // 👇️ this runs
  console.log('✅ string starts with hello');
} else {
  console.log('⛔️ string does NOT start with hello');
}

String.indexOf 方法返回字符串中子字符串第一次出现的索引。

如果子字符串不包含在字符串中,则返回 -1。

如果该方法返回 0,我们可以断定该字符串以子字符串开头。

此解决方案不如使用 startsWith 方法直接和可读,但如果我们必须支持 Internet Explorer,它就可以完成工作。


使用 Regex 检查字符串是否以指定的子字符串开头

要检查字符串是否以子字符串开头,请使用 test() 方法和匹配字符串开头的子字符串的正则表达式。 如果字符串以子字符串开头,测试方法将返回 true,否则返回 false

if (/^abc/.test('abc123')) {
  // 👇️ this runs
  console.log('✅ string starts with abc');
} else {
  console.log('⛔️ string does NOT start with abc');
}

我们使用 RegExp.test 方法检查字符串是否以特定子字符串开头。

如果正则表达式在字符串中匹配,则该方法返回 true,否则返回 false

正斜杠 // 标记正则表达式的开始和结束。插入符号 ^ 匹配输入的开头。

在此示例中,我们检查字符串 abc123 是否以子字符串 abc 开头。

如果在阅读正则表达式时需要帮助,请查看我们的正则表达式教程

如果要使正则表达式不区分大小写,请添加 i 标志。

// 👇️ with `i` flag
if (/^abc/i.test('ABC123')) {
  // 👇️ this runs
  console.log('✅ string starts with abc');
} else {
  console.log('⛔️ string does NOT start with abc');
}

i 标志允许我们在字符串中执行不区分大小写的搜索。

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

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便