0

I have a string like /233/ziyuanku/Screenshot_2014-09-03-16-11-45.png or /233/ziyuanku/37770506/edit.png

I want to remove the /233 and get the last string /ziyuanku/Screenshot_2014-09-03-16-11-45.png

My strings are like a /, a number, and a path, may have a number_name.

I can get the regexp '/\d+(.*)', but I don't know how to do it in JavaScript.

Please help. Thank you.

1
  • use the first capturing group to capture the result Commented Jun 12, 2016 at 3:43

3 Answers 3

2

Try this regex

function replaceName(url){

    var match = /^\/\d+(\/.*)$/.exec(url);
    if(match){
        return match[1];
    }

}

replaceName('/233/ziyuanku/Screenshot_2014-09-03-16-11-45.png');//returns /ziyuanku/Screenshot_2014-09-03-16-11-45.png

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

2 Comments

Thank you. And I have another question. How to remove the file_name and only save the path? Like /ziyuanku/Screenshot_2014-09-03-16-11-45.png to /ziyuanku/. I tried $ but didn't find the right way.
/^\/\d+(\/([^\/+]))(\/.*)$/ will give you the file name. match[2] is the name you wanted. match[3] Will give you the rest of the string.
0

You can apply a regex, as follows:

var name = "John/Smith";
var re = /([^A-Za-z])/g;
var subst = ' ';
name.replace(re, subst);

result: "John Smith"

Comments

0

try

str = '/233/ziyuanku/Screenshot_2014-09-03-16-11-45.png';
result = str.replace(/\/[0-9]+/, '');

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.