If you want to "exclude those urls that do not have a specific word in them", you can use a positive lookahead for the word (with some number of characters before it) e.g.
(?=.*Sing)
In Javascript:
const word = 'Sing';
const urls = ['http://I_like_to_sing.mp3', 'http://Another_song.mp3'];
let regex = new RegExp('https?:\/\/(?=.*' + word + ').+\.mp3', 'i');
console.log(urls.filter(v => v.match(regex)));
In PHP
$word = 'Sing';
$urls = ['http://I_like_to_sing.mp3', 'http://Another_song.mp3'];
$regex = "/https?:\/\/(?=.*$word).+\.mp3/i";
print_r(array_filter($urls, function ($v) use ($regex) { return preg_match($regex, $v); }));
Output:
Array (
[0] => http://I_like_to_sing.mp3
)
Demo on 3v4l.org
Update
To exclude those URLs that do have a specific word in them, you can use a negative lookahead instead e.g.
(?![^.]*Sing)
We use [^.]
to ensure the word occurs before the .mp3
part. Here's a PHP demo:
$word = 'Song';
$string = "some words http://I_like_to_sing.mp3 and then some other words http://Another_song.mp3 and some words at the end...";
$regex = "/(https?:\/\/(?![^.]*$word).+?\.mp3)/i";
preg_match_all($regex, $string, $matches);
print_r($matches[1]);
Output:
Array (
[0] => http://I_like_to_sing.mp3
)
Demo on 3v4l.org