0

i have a string,

mystr = 'public\uploads\file-1490095922739.jpg';

i want to replace

public\uploads

with " ", so that i just want to extract only file name ie

file-1490095922739.jpg

or like,

\uploads\file-1490095922739.jpg

how can i do this, is there any methods for this in js or can we do it by replace method.

i am performing the following steps,

var imagepath1;
var imagepath = 'public\uploads\file-1490095922739.jpg';
unwantedChar = 'public|uploads';
regExp = new RegExp(unwantedChar , 'gi');
imagepath = imagepath.replace(regExp , '');
imagepath1 = imagepath;
$scope.model.imagepath = imagepath1.replace(/\\/g, "");

please suggest me optimized method.

6
  • Is the part you are looking for always the part after the last `\` character? Commented Mar 21, 2017 at 12:06
  • no i want to remove the word "public\uploads" so that i remain with the filename "file.jpg". Commented Mar 21, 2017 at 12:10
  • So that is not the same thing? In other words, could public\uploads be followed by anything else than a single filename separated by a backslash? Commented Mar 21, 2017 at 12:12
  • yes exactly!!!!! Commented Mar 21, 2017 at 12:20
  • So what should happen if the input is \uploads\file-1490095922739.jpg? Commented Mar 21, 2017 at 12:24

5 Answers 5

1
var input = "public\\uploads\\file-1490095922739.jpg";
var result = input.replace("public\\uploads\\", "");

This is what you're looking for, no need for fancy regexs :). More information about replace can be found here.

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

Comments

0

Maybe I don't understand the issue - but wouldn't this work?

var mystr = 'public\uploads\file-1490095922739.jpg';
var filename = mystr.replace('public\uploads', '');

3 Comments

i tried the same but it is throwing the following error Uncaught SyntaxError: Invalid Unicode escape sequence
Ah yeah as @Dax pointed out - you need to escape the slashes. :-)
yeah that's working fine, need to escape the slashes :-)
0

If you want to get the part of the string after the last backslash character, you can use this:

var filename = mystr.substr(mystr.lastIndexOf('\\') + 1);

Also note that you need to escape the backslash characters in your test string:

var mystr = 'public\\uploads\\file-1490095922739.jpg';

Comments

0

What about just doing:

var imagepath = 'public\\uploads\\file-1490095922739.jpg';
$scope.model.imagepath = imagepath.replace('public\\uploads\\', '');

instead of using a bunch of unnecessary variables?

This way you're getting the file path, removing public\uploads\ and then setting the file path to $scope.model.imagepath

Note that this will only work if the image file path always matches 'public\uploads\*FILENAME*'.

1 Comment

Uncaught SyntaxError: Invalid Unicode escape sequence,,,,,,,,,, the above stated logic works fine
0
var url = '/anysource/anypath/myfilename.gif';
var filename = url.slice(url.lastIndexOf('/')+1,url.length);

Search for the last forward slash, and slice the string (+1 because you don't want the slash), with the length of the string to get the filename. This way, you don't have to worry about the path is at all times.

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.