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

Regex for searching URLS with excluding some words

I’m trying to search and replace all URLs in the text. However, I have some words that I need to exclude from the search. For example (?!word)(?:https?:\/\/)?(?:[a-zA-Z0-9.]+)\.(?:[a-zA-Z]{2,3}). This regex found all urls but not exclude urls which contain word. Please help me finish this regex.

I’m tried:

const text = `This is a sample text with links: www.example.com, https://www.hello.com, http://www.google.com, https://word.com, http://word.com
www.word.com https://www.site.frs www.word.com asasd.cds qdwdew.www asd.cdd 
sdsd https://www.word.com
https://www.word.frs some text hello word https www.example.wfd asdasd wwwsds.dsad.word.com 
`;

const regex = /(?!word)(?:https?:\/\/)?(?:[a-zA-Z0-9.]+)\.(?:[a-zA-Z]{2,3})/gm;

const matches = text.replace(regex, '[xxx]');;

console.log(matches);

I got this result:

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

This is a sample text with links: [xxx], [xxx], [xxx], [xxx], [xxx]
[xxx] [xxx] [xxx] [xxx] [xxx] [xxx] 
sdsd [xxx]
[xxx] some text hello word https [xxx] asdasd [xxx] 

I want to get this result:

This is a sample text with links: [xxx], https://www.hello.com, [xxx], https://word.com, http://word.com
www.word.com [xxx] www.word.com [xxx] [xxx] [xxx] 
sdsd https://www.word.com
https://www.word.frs some text hello word https [xxx] asdasd wwwsds.dsad.word.com

>Solution :

You could pass a callback function to String#replace instead, which checks if the match contains the given word to decide whether or not to replace.

const text = `This is a sample text with links: www.example.com, https://www.hello.com, http://www.google.com, https://word.com, http://word.com
www.word.com https://www.site.frs www.word.com asasd.cds qdwdew.www asd.cdd 
sdsd https://www.word.com
https://www.word.frs some text hello word https www.example.wfd asdasd wwwsds.dsad.word.com 
`;

const regex = /(?:https?:\/\/)?(?:[a-zA-Z0-9.]+)\.(?:[a-zA-Z]{2,3})/gm;

const matches = text.replace(regex, m => m.includes('word') ? m : '[xxx]');
console.log(matches);
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