gpt4 book ai didi

javascript - 匹配的正则表达式

转载 作者:行者123 更新时间:2023-12-03 02:10:21 25 4
gpt4 key购买 nike

我有这样的文字:

Habitación 101

正如你所看到的,有一个带有重音符号的字母:ó

我正在寻找一个正则表达式,当单词不带重音时也能匹配,即“habitacion”。

我正在使用 JavaScript 的 new RegExp(keyword, 'u')

最佳答案

那么只需使用 Habitaci[ó|o]n作为正则表达式。

const regex = new RegExp('Habitaci[ó|o]n', 'gi');

其中 [ó|o] 匹配列表 ó|o 中的单个字符。

这是一个演示:

const regex = new RegExp('Habitaci[ó|o]n', 'gi');
const str = `Habitación
Habitacion`;
let m;

while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}

// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}

关于javascript - 匹配的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49607668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com