我试图从脚本中获取特殊的单词,在维基百科中搜索,以获得解释。
输入文本:“泰语,或中泰语,是泰语系的泰语,由泰国中部人民和绝大多数泰国人讲,是泰国唯一的官方语言。我想……”
预期输出:“泰语”、“中部泰语”、“Kra-Dai”、“泰语”、“泰国”。
然后,通过使用Wikipedia API,我将得到上述单词的定义。我使用这个正则表达式:
[A-Z][-a-zA-Z]*(?:\s+[A-Z][-a-zA-Z]*)?然而,当我尝试时,结果是:
“泰语”、“中泰语”、“克拉”、“戴”、“泰语”、“泰国”、“我”、“它”
它将包含"-“的单词分隔开,并包括以点之后的上部开始的单词。也包括“我”和“它”。
除了“”之后的大写单词之外,我怎么能得到所有的大写单词。
发布于 2022-02-14 12:08:19
我们可以使用单词边界\b。
let str = 'Thai, or Central Thai, is a Tai language of the Kra-Dai language family spoken by the Central Thai people and a vast majority of Thai Chinese. It is the sole official language of Thailand. I want to...';
let arr =[...str.matchAll( /\b[A-Z]\w{2,}-?(\s?\b[A-Z]\w*)?/g)].map(e=>e[0]);
console.log(arr);
,除了大写单词“”之后,我怎么能得到所有的大写单词。
但你也可以在句子的开头找到一些特殊的词:
Periplectic group consists of the group's last common ancestor and all its descendants
发布于 2022-02-14 12:20:10
这对我有效,(?!.\s)[A-Z][a-z]+(?:\s[A-Z][a-z]+|[–]+[A-Z][a-z]*|[a-z]+)
https://stackoverflow.com/questions/71109604
复制相似问题