Help me with the regex

Hello,

How do you search a text which has many sentences, when you are expected to get resulting strings like below?

“ended up being”
“ended up having”
“ended up spending”
“ended up getting”

They all have “ended up ~ing” pattern, so I was wondering which regex would be appropriate or near perfect for the search I am talking about.

Thank you.

const { ended_up, ing_word } = 'ended up being'.match(/(?<ended_up>ended\sup\s)(?<ing_word>[^ ]+ing)/).groups

This uses regex’s groups & destructuring assignment.

I’ve created two groups: <ended_up> & <ing_word>

First regex grabs everything from 'ended up[space]'
Second regex grabs everything not a space up to the characters 'ing'.

I would have done a literal match for the unchanging parts, and then \w or [a-z] with a counter for one or more times for the beginning of the verb - but I am no expert in regex

One example text would be like this:

http://1novels.com/241082-frostbite.html

Thank you.

I just want to know how people are using the “ ended up ~ing” patterns, because I am still learning English.

Maybe, I could build a little English usage dictionary for myself.

It would be great if I could get the entire sentences that contains the pattern.

If that is near impossible, I might be happy to grab “ended up ~ing” parts, so that I can highlight them manually or programmatically.

As a minimum, I would like to get the patterned parts.

For your example:

const story = document.querySelector('#game-width > div > div.chapter-content-p > p:nth-child(2)').innerText;

story.match(/[A-Za-z][A-Za-z ]+ended\sup\s[A-Za-z]+ing\s\w+./g)

// ["She ended up getting staked."]

And if you want to combine the two suggestions:

const matches = [...story.matchAll(/(?<sentence>[A-Za-z][A-Za-z ]+ended\sup\s(?<ing_word>[A-Za-z]+ing)\s(?<verb>\w+).)/g)];

const sentences = matches.map((_match) => _match.groups.sentence);
const ing_words = matches.map((_match) => _match.groups.ing_word);
const verbs = matches.map((_match) => _match.groups.verb);

Enjoy.

Randell introduced an interesting possibility where the verb part has multi parts.

const matches = [...story.matchAll(/(?<sentence>[A-Z][A-Za-z ]+ended\sup\s(?<ing_word>[A-Za-z]+ing)\s(?<verb>[A-Za-z ]+).)/g)]

Would account for that.

Both of you guys helped me a lot. Thank you very much for helping me out.
I really appreciate it.