Help me with the regex


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.

Can you give an actual example of the text string and the results you would want to capture? I think I understand what you want, but an example could solidify my assumptions.

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:

Thank you.

What is the ultimate goal once you have identified something like ended up getting staked which appears in your example text. Are you wanting to replace a portion of it with something else?

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 = => _match.groups.sentence);
const ing_words = => _match.groups.ing_word);
const verbs = => _match.groups.verb);


If you had the following text:

Tom ended up spending his money. That was when Victor made his real move, kidnapping her and torturing her until she gave into his demands. Tony ended up dying. In the process, he took some pretty extreme measures¡ªlike zapping me and Dimitri, my mentor, with a lust spell. (I’ll get to him later). Victor also exploited the way spirit was starting to make Lissa mentally unstable. But even that wasn’t as bad as what he did to his own daughter Natalie. He went so far that she ended up moving out to encourage her to turn into a Strigoi to help cover his escape. She ended up getting staked again. Even when captured after the fact, Victor didn’t seem to display too much guilt over what he’d asked her to do. Makes me think I wasn’t missing out on growing up without a father.

then you could use something like the following to get all (or most) sentences which match the pattern you want:

const regex = /([a-z]+[,;]? )+ended up [a-z]+ing( [a-z]+)+\./gi;

The above would create the following array of sentences:

  'Tom ended up spending his money.',
  'Tony ended up dying.',
  'He went so far that she ended up moving out to encourage her to turn into a Strigoi to help cover his escape.',
  'She ended up getting staked again.' 

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.