2

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.

5 Answers 5

3

You may use this regex:

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

RegEx Demo

RegEx Breakdown:

  • ^: Start
  • [A-Za-z]+: Match 1+ of English letters
  • (?:\s+[A-Za-z]+)*: Match 1+ whitespaces followed by 1+ of English letters. Repeat this group 0 or more times.
  • (?=:): Lookahead to assert that we have : at next position
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

I like my Regex's as short as possible so here's my take.

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

Breakdown:

  • ^ asserts start of line
  • \S matches any non-whitespace character.
  • [^:]+ matches all character which isn't : one or unlimited times.

Comments

0

You can do it as above:

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

enter image description here

1 Comment

Your answer looks below answer. stackoverflow.com/a/74741597/7901430 What's different?
0

In your pattern [A-Za-z]+(?=\:\S) you are not matching any spaces in case of multiple words, and you are asserting a colon to the right followed by a non whitespace char using (?=\:\S)

If there should be a part with a colon and parenthesis following and using a capture group:

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

Explanation

  • ^ Start of string
  • ([A-Za-z].*?) Capture group 1, match a char A-Za-z followed by any char except a newline, as least as possible
  • :\s* Match a colon and optional whitespace chars
  • \([^()]*\) Match from (...)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.