gpt4 book ai didi

javascript - 正则表达式用于匹配句子中间的名字

转载 作者:行者123 更新时间:2023-11-30 14:02:54 25 4
gpt4 key购买 nike

我正在 try catch 段落中的名称,并将它们作为数组返回。带有名称的句子包含“names are”。示例:

The first sentence. Some second sentence. Third sentence and the names are John, Jane, Jen. Here is the fourth sentence about other stuff.

期望的输出:

[“约翰”、“简”、“仁”]

尝试

paragraph.match(/names are ([A-Za-z ]+,{0,1} {0,1})+\./)

最佳答案

您可以使用 names are ([^.]+) 来匹配下一个句点之前的所有内容。然后使用 split将名称获取到数组

const str = 'The first sentence. Some second sentence. Third sentence and the names are John, Jane, Jen. Here is the fourth sentence about other stuff.'

const regex = /names are ([^.]+)/,
names = str.match(regex)[1],
array = names.split(/,\s*/)

console.log(array)

关于javascript - 正则表达式用于匹配句子中间的名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55994978/

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