Catscarlet
Catscarlet

Reputation: 589

How to use regexp to replace this string in JS?

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.

Upvotes: 0

Views: 46

Answers (3)

irimawi
irimawi

Reputation: 368

try

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

Upvotes: 0

Bryan Euton
Bryan Euton

Reputation: 929

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

Upvotes: 2

Claudio
Claudio

Reputation: 51

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"

Upvotes: 0

Related Questions