Home > Net >  How to modify the regex
How to modify the regex

Time:12-09

I have a text as below and some words and their meanings are included in the text.I need to get these words using a Javascript regex method. For example, if I give below text than return words should be Necessity, Lay of the land, and Mumble.

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.

I have coded as below but it doesn't work as expected.

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']

May I know what am I missing? Any help would be appreciated.

CodePudding user response:

Try this version:

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);

Here is an explanation of the regex pattern used:

  • (
    • ^ the start of the string
    • | OR
    • (?<=\n\n) lookbehind and assert two newlines precede
  • )
  • \w match a word
  • (?: \w )* followed by space and another word, zero or more times
  • (?=:) assert that : follows this word or words

CodePudding user response:

You may use this regex:

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

enter image description here

  • Related