3

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!

9
  • Uncaught ReferenceError: path is not defined What is path? Commented Feb 20, 2019 at 6:27
  • maybe make '/[")(]/g' a regular expression, not a string by remove the quotes file.replace(/[")(]/g,'') Commented Feb 20, 2019 at 6:28
  • Which characters do you want to remove? Commented Feb 20, 2019 at 6:31
  • @ CertainPerformance you've right, ive update the post @ Jack Bashford at the end i want to have only the filename, so i must remove the path and the url("") Commented Feb 20, 2019 at 6:33
  • What do you want from the path string? Is "myfilename.jpg" what you'd like to have as a result? Commented Feb 20, 2019 at 6:34

3 Answers 3

2

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?

Sign up to request clarification or add additional context in comments.

2 Comments

yes, that was the problem, i was in faith, that the regex must be in quotes
Yeah.. right..i also find the issue.. But @mikeD u can use better approach for this problem. Something like path.split('/').pop().split('"')[0]; or path.split('"') console.log(file[1].split('/')[file[1].split('/').length -1]) or so on and so fourth...
1

Correct regex to use here would be /[()"]*/g instead of '/[")(]/g'

2 Comments

file=file.replace('/[()"]*/g',''); brings me the same result like in my post
Notice quotes around the regex. Hope you have already got that.
1

const path = 'url("https://www.example.com/folder/next/another/myfilename.jpg")';

const filename = path.split('/').pop().split('"')[0];
console.log(filename);

Method

  1. Split your string based on /
  2. Get the last item with pop. This will give you myfilename.jpg")
  3. Split again with "
  4. Get the first item with array[0]. This will give you myfilename.jpg

Functions

  • Use Array.split to split your string based on a delimiter. The first time it's the /, the second time it's the " character
  • Use Array.pop to get (and remove) the last item in an array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.