开发者

JavaScript RegEx to change a string

开发者 https://www.devze.com 2023-01-06 09:32 出处:网络
I need to take strings in the following format: http:\\/\\/img.mypath.net\\/time\\/daily\\/2010\\/1006\\/my_image_name.jpg

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消