当前位置:首页 > JavaScript

js实现匹配

2026-04-06 23:33:19JavaScript

JavaScript 实现字符串匹配

JavaScript 提供了多种方法来实现字符串匹配,包括使用内置方法和正则表达式。

使用 String.prototype.includes() 方法

const str = "Hello, world!";
const substring = "world";
const isMatch = str.includes(substring); // true

includes() 方法检查字符串是否包含指定的子字符串,返回布尔值。

使用 String.prototype.indexOf() 方法

const str = "Hello, world!";
const substring = "world";
const position = str.indexOf(substring); // 7

indexOf() 方法返回子字符串首次出现的索引,未找到则返回 -1。

使用 String.prototype.match() 方法

const str = "Hello, world!";
const regex = /world/;
const result = str.match(regex); // ["world", index: 7, input: "Hello, world!"]

match() 方法使用正则表达式匹配字符串,返回匹配结果数组或 null。

使用 String.prototype.search() 方法

const str = "Hello, world!";
const regex = /world/;
const position = str.search(regex); // 7

search() 方法使用正则表达式搜索字符串,返回匹配的索引或 -1。

使用 RegExp.prototype.test() 方法

const str = "Hello, world!";
const regex = /world/;
const isMatch = regex.test(str); // true

test() 方法测试正则表达式是否匹配字符串,返回布尔值。

js实现匹配

使用 RegExp.prototype.exec() 方法

const str = "Hello, world!";
const regex = /world/;
const result = regex.exec(str); // ["world", index: 7, input: "Hello, world!"]

exec() 方法在字符串中执行正则表达式搜索,返回匹配结果数组或 null。

正则表达式高级匹配

匹配多个结果

const str = "The rain in Spain falls mainly in the plain";
const regex = /ain/g;
const matches = str.match(regex); // ["ain", "ain", "ain"]

使用全局标志 g 可以匹配所有符合条件的子字符串。

使用捕获组

const str = "John Doe";
const regex = /(\w+)\s(\w+)/;
const result = regex.exec(str);
// ["John Doe", "John", "Doe", index: 0, input: "John Doe"]

圆括号 () 用于创建捕获组,匹配的子字符串会被单独返回。

js实现匹配

不区分大小写匹配

const str = "Hello, World!";
const regex = /world/i;
const isMatch = regex.test(str); // true

使用 i 标志进行不区分大小写的匹配。

性能考虑

对于简单的子字符串检查,includes()indexOf() 通常比正则表达式更快。正则表达式适合更复杂的模式匹配场景。

字符串方法 vs 正则表达式

  • 字符串方法:简单、高效,适合固定子字符串匹配
  • 正则表达式:功能强大,适合模式匹配和复杂规则

实际应用示例

验证电子邮件格式

function isValidEmail(email) {
    const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return regex.test(email);
}

提取URL中的域名

function getDomain(url) {
    const regex = /^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/;
    const match = url.match(regex);
    return match ? match[1] : null;
}

替换字符串中的模式

const str = "The quick brown fox jumps over the lazy dog";
const newStr = str.replace(/fox/, "cat"); // "The quick brown cat jumps over the lazy dog"

标签: js
分享给朋友:

相关文章

js实现继承

js实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js实现二叉树

js实现二叉树

二叉树的基本概念 二叉树是一种树形数据结构,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的常见操作包括插入、删除、遍历等。 二叉树的节点定义 在JavaScript中,二叉树的节点可…

js实现防洪

js实现防洪

防抖(Debounce)实现 防抖的核心思想是在事件触发后延迟执行回调函数,若在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口调整等场景。 function debounce(func,…

js实现投球

js实现投球

实现投球动画的基本思路 使用JavaScript和CSS动画结合的方式模拟投球效果。核心是通过改变元素的位置、旋转和缩放属性,配合定时器或CSS过渡实现平滑动画。 创建基础HTML结构 <di…

js实现下拉菜单

js实现下拉菜单

使用HTML和CSS创建基础结构 HTML部分需要包含一个触发下拉的按钮和隐藏的下拉菜单内容: <div class="dropdown"> <button class="dr…

js 实现跳转

js 实现跳转

使用 window.location.href 进行跳转 通过修改 window.location.href 可以跳转到指定 URL,浏览器会加载新页面: window.location.hre…