javascript 如何修改正则表达式

tvz2xvvm  于 2022-12-10  发布在  Java
关注(0)|答案(5)|浏览(141)

我有一个文本如下和一些词和他们的意思包括在文本中。我需要得到这些词使用Javascript正则表达式的方法。例如,如果我给予下面的文本比返回的话应该是Necessity, Lay of the land, and Mumble
Necessity:(名词)对某事的需要,或被需要的事物。例如:在我的工作中,电脑是必需品。
地形:(习语或动词)被考虑的事物的一般状态或状况;情况的事示例如:我们问了几个问题了解一下情况
(动词)小声地或不清楚地说,使人难以理解。例如:她咕哝了几句需要回家,然后就走了。
我已经编码如下,但它并不像预期的那样工作。

let text = `Necessity:(noun)the need for something, or something that is needed
Example: In my work, a computer is a necessity.

Lay of the land: (idiom n. Or v.)the general state or condition of affairs under consideration; the facts of a situation
Example: We asked a few questions to get the lay of the land.

Mumble:(verb) to speak quietly or in an unclear way so that the words are difficult to understand
Example: She mumbled something about needing to be home, then left.
`;

let matches = text.match(/[A-Za-z]+(?=\:\S)/g);

console.log(matches); //['Necessity', 'Mumble']

我能知道我错过了什么吗?任何帮助都将不胜感激。

33qvvth1

33qvvth11#

您可以使用此正则表达式:

/^[A-Za-z]+(?:\s+[A-Za-z]+)*(?=:)/gm

RegEx Demo

RegEx细分:

  • ^:开始
  • [A-Za-z]+:匹配1个以上的英语字母
  • (?:\s+[A-Za-z]+)*:匹配1+个空格,后跟1+个英语字母。重复该组0次或更多次。
  • (?=:):LookaheadAssert在下一个位置有:
jw5wzhpr

jw5wzhpr2#

尝试此版本:

let text = `Necessity:(noun)the need for something, or something that is needed
Example: In my work, a computer is a necessity.

Lay of the land: (idiom n. Or v.)the general state or condition of affairs under consideration; the facts of a situation
Example: We asked a few questions to get the lay of the land.

Mumble:(verb) to speak quietly or in an unclear way so that the words are difficult to understand
Example: She mumbled something about needing to be home, then left.
`;

let matches = text.match(/(^|(?<=\n\n))\w+(?: \w+)*(?=:)/g);

console.log(matches);

下面是对所用正则表达式模式的解释:

  • (
  • ^字符串的开头
  • |
  • (?<=\n\n) lookbehind并在前面Assert两个换行符
  • )
  • \w+匹配单词
  • (?: \w+)*后跟空格和另一个单词,零次或多次
  • (?=:)Assert:跟在这个或这些字后面
mwyxok5s

mwyxok5s3#

我喜欢我的正则表达式尽可能短,所以这是我的看法。

const text = `Necessity:(noun)the need for something, or something that is needed Example: In my work, a computer is a necessity.

Lay of the land: (idiom n. Or v.)the general state or condition of affairs under consideration; the facts of a situation Example: We asked a few questions to get the lay of the land

Mumble:(verb) to speak quietly or in an unclear way so that the words are difficult to understand Example: She mumbled something about needing to be home, then left.`;

let matches = text.match(/^\S[^:]+/gm);
console.log(matches);

细目分类:

  • ^置位行首
  • \S匹配任何非空白字符。
  • [^:]+匹配不是:的所有字符一次或无限次。
dxxyhpgq

dxxyhpgq4#

在模式[A-Za-z]+(?=\:\S)中,如果有多个单词,则不匹配任何空格,并且使用(?=\:\S)Assert右边的冒号后跟非空格字符
如果应该有一个带有冒号和括号的部分跟随并使用捕获组:

^([A-Za-z].*?):\s*\([^()]*\)

说明

  • ^字符串开头
  • ([A-Za-z].*?)捕获组1,尽可能匹配字符A-Za-z后跟除换行符以外的任何字符
  • :\s*匹配冒号和可选空格字符
  • \([^()]*\)(...)匹配

请参见regex demo

const regex = /^([A-Za-z].*?):\s*\([^()]*\)/gm;
const str = `Necessity:(noun)the need for something, or something that is needed Example: In my work, a computer is a necessity.

Lay of the land: (idiom n. Or v.)the general state or condition of affairs under consideration; the facts of a situation Example: We asked a few questions to get the lay of the land

Mumble:(verb) to speak quietly or in an unclear way so that the words are difficult to understand Example: She mumbled something about needing to be home, then left.`;

const result = Array.from(str.matchAll(regex), m => m[1]);
console.log(result);
bmp9r5qi

bmp9r5qi5#

您可以按照上面的方法进行操作:

let regex_exp = /(^|(?<=\n\n))\w+(?: \w+)*(?=:)/g;
let matches = text.match(regex_exp);
console.warn(matches);

相关问题