Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need a regex in js able to capture the word after 'cat '

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

For example:

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading