JavaScript RegEx to change a string
I need to take strings in the following format:
http:\/\/img.mypath.net\/time\/daily\/2010\/1006\/my_image_name.jpg
And convert them to开发者_JAVA百科 this, using JavaScript:
http://www.mynewpath.com/i/daily/2010/1006/77_my_image_name.jpeg
I'm sure that someone more fluent in RegEx than I can give a terse solution.
How about this?
var url = 'http:\/\/img.mypath.net\/time\/daily\/2010\/1006\/my_image_name.jpg';
url = url.replace('\\/', '/'); // Replace \/ by /
url = url.replace('img.mypath.net/time', 'www.mynewpath.com/i'); // Replace domain and first path.
url = url.replace(/([^/]+)$/g, '77_$1'); // Prefix last path with `77_` (???)
alert(url);
The requirement around 77
is unclear, but if it's fixed, the above should do.
精彩评论