Home  >  Q&A  >  body text

How to get word after regex matching phrase -

I want to get the next word after the matching phrase.

wait. - I want to change my pain threshold

--"Change my ***"

let text = "I want to change my pain threshold"
    let firstTriggerregEx = "(?:\b|')(change my)(?:\b|')";
    const found2 = text.match(firstTriggerregEx);
    console.log(found2);

This will return the match, but not the following word

P粉834840856P粉834840856376 days ago518

reply all(1)I'll reply

  • P粉466909449

    P粉4669094492023-09-13 00:05:44

    You can use the following modes:

    (?<=\b改变我的)(\w+)

    The first part is a positive review. It ensures that the following characters/groups are preceded by the specified pattern.

    The second part consists of the "word character" ([a-zA-Z0-9_]) character set in the capturing group so that you can retrieve it later.

    https://regex101.com/r/CdKcVh/1

    reply
    0
  • Cancelreply