gpt4 book ai didi

javascript - 仅考虑字母和数字在基本字符串中查找句子

转载 作者:行者123 更新时间:2023-12-01 00:06:35 26 4
gpt4 key购买 nike

我正在尝试找到一个与此类似的句子:

I'm a sentence with some text and number and so on 45

在看起来像这样的基本字符串中,如 Markdown 文本:

Here is some content \n \n > Listing some items: - Hello, I'm *** bold ***. \n \n I'm a \n
\n sentence with - some text and number and so on 45

我遇到了this使用正则表达式的问题,但我无法让它在我的示例中工作。我也做了一些这样的改变,但没有成功:

const content = "Here is some content \n \n > Listing some items: - Hello, I'm *** bold ***. \n \n I'm a \n \n sentence with - some text and    number and so on 45"


const search = "I'm a sentence with some text and number and so on 45"

const regex = '[^a-zA-Z0-9]*?';
const searchTerm = search.split("").join(regex);

var matches = content.match(searchTerm);
console.log(matches);

我尝试了不同的正则表达式变量,例如:“[^a-zA-Z0-9]?”、“[^\w\d\s]”和“[.\-] '

提前致谢

最佳答案

您可以做的一件事是通过删除不需要的字符来创建您正在搜索的字符串的干净版本(我保留单引号,因为您匹配“I'm”)。然后,您可以对清理后的内容进行简单的字符串搜索。

代码笔:https://codepen.io/edlucas/pen/eYNdXLq

const content = "Here is some content \n \n > Listing some items: - Hello, I'm *** bold ***. \n \n I'm a \n \n sentence with - some text and    number and so on 45";

const search = "I'm a sentence with some text and number and so on 45";

// Remove the unwanted characters and then all multiple spaces
newContent = content.replace(/[^\w' ]/g, '').replace(/[\s]+/g,' ');

console.log(newContent);

const matches = newContent.includes(search);

console.log('Matches?', matches);

关于javascript - 仅考虑字母和数字在基本字符串中查找句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60328715/

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