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:
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);