gpt4 book ai didi

javascript - 协助正则表达式字符串匹配 javascript,特殊情况

转载 作者:行者123 更新时间:2023-11-30 09:17:09 24 4
gpt4 key购买 nike

您好,我有一个 JSON 数据库,我需要在其中匹配用户输入和关键字。除了一个特例,我在这方面取得了成功。基本上,如果用户输入“icecream”,则应该与关键字或字符串“ice cream”相匹配。

我尝试删除“ice cream”中的空格以使其成为“icecream”,但它随后否定了很多其他匹配项,例如“ice cream cone”变成了“icecreamcone”。是否有适用于这种情况的正则表达式?

var input = new RegExp("icecream", "i");
var keywords = "ice cream, ice cream cone, plastic container lid";

if (keywords.search(input) != -1) {

//do Something

}

如果我要搜索“plastic lid”而不是“plastic container lid”,它也应该在“icecream”和“ice cream”之间找到匹配项,后者也应该找到匹配项。任何帮助是极大的赞赏。最终,我正在寻找一种能够解决所有情况的解决方案,而不仅仅是“冰淇淋”与“冰淇淋”。

最佳答案

Tl;dr

var input = new RegExp('icecream'.split('').join('(\\s)*').concat('|icecream'), 'i');
var keywords = "ice cream, ice cream cone, plastic container lid";

if (keywords.search(input) != -1) {

//do Something

}

完整答案:

为了回答您的问题,我提出了以下方法:

function makeRegEx(input) {
// The regex for an optional whitespace.
let glueRegex = '(\\s)*';

// Transform the string into an array of characters.
let splittedString = input.split('');

// Join the characters together, with the optional whitespace inbetween.
let joinedString = splittedString.join(glueRegex)

// Add the actual input as well, in case it is an exact match.
joinedString += '|' + input;

// Make a new regex made out of the joined string.
// The 'i' indicates that the regex is case insensitive.
return new RegExp(joinedString, 'i');
}

这将创建一个新的正则表达式,在每个字符之间放置一个可选的空格。

这意味着对于给定的字符串 icecream,您最终会得到如下所示的 RegEx:

/i(\s)*c(\s)*e(\s)*c(\s)*r(\s)*e(\s)*a(\s)*m/我

此正则表达式将匹配以下所有情况:

  • 我是奶油
  • 冰淇淋
  • 冰淇淋 <= 这是你的!
  • icec 令
  • 冰激凌
  • 我是冰激凌
  • 冰激凌
  • 冰淇淋

整个方法也可以简化为:

let input = new RegExp(input.split('').join('(\\s)*').concat(`|${input}`), 'i');

它很短,但也很难读。


集成到您的代码中,它看起来像这样:

function makeRegEx(input) {
// The regex for an optional whitespace.
let glueRegex = '(\\s)*';

// Transform the string into an array of characters.
let splittedString = input.split('');

// Join the characters together, with the optional whitespace inbetween.
let joinedString = splittedString.join(glueRegex)

// Add the actual input as well, in case it is an exact match.
joinedString += '|' + input;

// Make a new regex made out of the joined string.
// The 'i' indicates that the regex is case insensitive.
return new RegExp(joinedString, 'i');
}



let userInput = 'icecream';
let keywords = "ice cream, ice cream cone, plastic container lid";

let input = makeRegEx('icecream');

// Check if any of the keywords match our search.
if (keywords.search(input) > -1) {
console.log('We found the search in the given keywords on index', keywords.search(input));
} else {
console.log('We did not find that search in the given keywords...');
}

或者这个:

var input = new RegExp('icecream'.split('').join('(\\s)*').concat('|icecream'), 'i');
var keywords = "ice cream, ice cream cone, plastic container lid";

if (keywords.search(input) != -1) {

//do Something

}

关于javascript - 协助正则表达式字符串匹配 javascript,特殊情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54294937/

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