迹忆客 专注技术分享

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

JavaScript 中的通配符字符串比较

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

这篇文章是关于 JavaScript 的正则表达式,不同的字符串方法,以及 JavaScript 中的通配符字符串比较。这些问题被解决为:

正则表达式是由一系列字符组成的搜索模式。此搜索模式可以定义你在搜索文本中的数据时要查找的内容。

正则表达式可能像单个字符一样简单,也可能像复杂模式一样复杂。所有文本搜索和文本替换操作都可以使用正则表达式来执行。

/BlocTAK/x;

在代码行中,BlocTAK 是用于搜索的模式,x 是确保搜索不区分大小写的修饰符。

RegExp 表达式方法是 test() 方法。它在字符串中查找模式并根据结果返回 true 或 false。

代码 - HTML:

<html>
<body>

<p>Search for "H" in the next paragraph:</p>
<p id="5">Hello World</p>
<p id="data"></p>

</body>
</html>

它也可以使用 <script> 标签添加到 HTML 的标题中。

代码 - JavaScript:

let text = document.getElementById("5").innerHTML;
const pattern = /e/;
document.getElementById("data").innerHTML = pattern.test(text);

函数 substring() 从字符串中提取字符并返回两个索引(位置)之间的子字符串。substring() 函数从字符串的开头到结尾删除字符(不包括在内)。

substring() 方法不会改变原始字符串。如果开始大于结束,则切换参数,并且当开始或结束值小于零时,它们被认为是 0

代码 - HTML:

<html>
<body>

<p>substring() extracts a part of a string:</p>
<p id="demo"></p>

</body>
</html>

代码 - JavaScript:

let text = "Hello world!";
let result = text.substring(7, 5);

document.getElementById("demo").innerHTML = result;

substr() 函数接受一个字符串段并提取它。substr() 函数接受给定数量的字符并返回定义数量的字符。

substr() 不会更改原始字符串。使用负开始位置从字符串的末尾删除字符。

代码 - HTML:

<html>
<body>

<p>substr() extracts a part of a string:</p>
<p id="data"></p>

</body>
</html>

代码 - JavaScript:

let text = "Hello world!";
let result = text.substr(7, 5);

document.getElementById("data").innerHTML = result;

你应该小心不要混淆 substring()substr() 方法,因为它们之间存在细微差别。substring() 有两个参数,开始和结束索引。

相比之下,substr() 有两个参数,起始索引和返回字符串中包含的字符数。

代码:

let text = 'BlocTAK'
console.log(text.substring(2,5)) // => "ocT"
console.log(text.substr(2,3))   // => "ocT"

单个字符,例如星号 (*),是一个通配符,可以读取为多个文字字符或空字符串。它经常用于文件搜索,因为它消除了输入完整名称的需要。

使用文本和通配符模式实现通配符模式匹配方法,以查看通配符模式是否与文本匹配。匹配应该一直贯穿文本。

?* 可用于通配符模式。

代码:

function match(first, second)
{
    if (first.length == 0 && second.length == 0)
        return true;

    if (first.length > 1 && first[0] == '*' &&
        second.length == 0)
        return false;

    if ((first.length > 1 && first[0] == '?') ||
        (first.length != 0 && second.length != 0 &&
        first[0] == second[0]))
        return match(first.substring(1),
                    second.substring(1));

    if (first.length > 0 && first[0] == '*')
        return match(first.substring(1), second) ||
            match(first, second.substring(1));

    return false;
}

function test(first, second)
{
    if (match(first, second))
    document.write("Yes" + "<br>");
    else
    document.write("No" + "<br>");
}

test("He*lo", "Hello"); // Yes
test("He?lo*", "HelloWorld"); // Yes
test("*pqrs", "pqrst"); // No because 't' is not in first
test("abc*bcd", "abcdhghgbcd"); // Yes
test("abc*c?d", "abcd"); // No because second must have 2 instances of 'c'

在移至 Pattern and Text 中的下一个字符之前,通配符模式存在三种情况。

上面的代码是一个字符串匹配的例子,其中给出了一个包含通配符的字符串,使用前两种情况。

使用 escapeRegex 函数转义正则表达式中具有任何特殊含义的任何字符。

代码:

function matchRuleShort(str, rule) {
  var escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
  return new RegExp("^" + rule.split("*").map(escapeRegex).join(".*") + "$").test(str);
}

function matchRuleExpl(str, rule) {

  var escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");

  rule = rule.split("*").map(escapeRegex).join(".*");

  rule = "^" + rule + "$"

  var regex = new RegExp(rule);

  return regex.test(str);
}
alert(
    "1. " + matchRuleShort("bird123", "bird*") + "\n" +
    "2. " + matchRuleShort("123bird", "*bird") + "\n" +
    "3. " + matchRuleShort("123bird123", "*bird*") + "\n" +
    "4. " + matchRuleShort("bird123bird", "bird*bird") + "\n" +
    "5. " + matchRuleShort("123bird123bird123", "*bird*bird*") + "\n" +
    "6. " + matchRuleShort("s[pe]c 3 re$ex 6 cha^rs", "s[pe]c*re$ex*cha^rs") + "\n" +
    "7. " + matchRuleShort("should not match", "should noo*oot match") + "\n"
);

escapeRegex 转义正则表达式中包含任何特殊含义的任何字符,对于任何字符串 grep(regexpEscape(string), string),它始终为 true

在本文中,我们介绍了正则表达式及其对象的概念。然后我们关注字符串方法 substrsubstring 及其区别。

此外,我们在 JavaScript 中将这些字符串方法 substr()substring() 与通配符方法一起使用。最后,我们在 JavaScript 中使用 regexp 对象 true()regexpEscape 函数使用通配符字符串比较。

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便