skip to Main Content

i need a regex able to capture the word after ‘cat ‘ (without taking ‘cat’)

For example:

In ‘cat gifi’ i want to capture ‘gifi’

I tried : /cat s+(w+)/
I also search on the internet and even ask to ChatGPT

Can you help me please ?

2

Answers


  1. You can use a lookbehind assertion, (?<=...) to assert that something needs to be present for the regex to match (here, cats+), without making it part of the actual match result.

    For example:

    const example = 'a cat gifi picture';
    
    const regex = /(?<=cats+)(w+)/;
    console.log(example.match(regex));

    will log ["gifi", "gifi"] (the entire regex match is gifi and the only capture group is gifi).

    Login or Signup to reply.
  2. (?<=cat[ ]+)(w)+
    

    add new lines, tabs etc to or condition in bracket if need be. I think this should work though.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search