I tried to remove some character from a string, but it failed and I don't know why.
This is the code
path='url("https://www.example.com/folder/next/another/myfilename.jpg")';
var file = path.split('/');
console.log('file is: '+file);
file = file[file.length-1];
console.log('file is: '+file);
file=file.replace('/[")(]/g',''); // try also replace('/[")(]','') failed
console.log('file is: '+file);
return file;
In the console I read
file is: url("https:,,www.example.com,folder,next,another,myfilename.jpg")
file is: myfilename.jpg")
file is: myfilename.jpg")
I don't understand why the "() charachters by the replace function won't replaced.
Thanks for helping and explaining!
Remove quotes in your regex:
function testFunction() {
path='url("https://www.example.com/folder/next/another/myfilename.jpg")';
var file = path.split('/');
console.log('file is: '+file);
file = file[file.length-1];
console.log('file is: '+file);
file=file.replace(/[")(]*/g, '');
console.log('file is: '+file);
return file;
}
testFunction();
In console:
file is: url("https:,,www.example.com,folder,next,another,myfilename.jpg")
file is: myfilename.jpg")
file is: myfilename.jpg
Return value is "myfilename.jpg" now.
Is it what youwhant?
Correct regex to use here would be /[()"]*/g instead of '/[")(]/g'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With