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 ?
>Solution :
You can use a lookbehind assertion, (?<=...) to assert that something needs to be present for the regex to match (here, cat\s+), without making it part of the actual match result.
For example:
const example = 'a cat gifi picture';
const regex = /(?<=cat\s+)(\w+)/;
console.log(example.match(regex));
will log ["gifi", "gifi"] (the entire regex match is gifi and the only capture group is gifi).