admin管理员组

文章数量:1022679

I'm trying to replace a part of the url with another string in javascript, so for example i have the following:

res/icons/android/quickshop/icon-36-ldpi.png
res/icons/android/quickshop/icon-48-mdpi.png
res/icons/ios/quickshop/icon-72-hdpi.png
res/icons/ios/quickshop/icon-96-xhdpi.png

and I want to replace it to the following (quickshop is dynamic and can be any series of characters, mostly [a-z][A-Z])

res/icons/android/homecenter/icon-36-ldpi.png
res/icons/android/homecenter/icon-48-mdpi.png
res/icons/ios/homecenter/icon-72-hdpi.png
res/icons/ios/homecenter/icon-96-xhdpi.png

I was always very lousy when it es to regular expressions, anyone could help?

I'm trying to replace a part of the url with another string in javascript, so for example i have the following:

res/icons/android/quickshop/icon-36-ldpi.png
res/icons/android/quickshop/icon-48-mdpi.png
res/icons/ios/quickshop/icon-72-hdpi.png
res/icons/ios/quickshop/icon-96-xhdpi.png

and I want to replace it to the following (quickshop is dynamic and can be any series of characters, mostly [a-z][A-Z])

res/icons/android/homecenter/icon-36-ldpi.png
res/icons/android/homecenter/icon-48-mdpi.png
res/icons/ios/homecenter/icon-72-hdpi.png
res/icons/ios/homecenter/icon-96-xhdpi.png

I was always very lousy when it es to regular expressions, anyone could help?

Share Improve this question edited Apr 15, 2016 at 21:57 Andrea Corbellini 17.8k3 gold badges58 silver badges71 bronze badges asked Apr 15, 2016 at 21:49 Yehia A.SalamYehia A.Salam 2,0289 gold badges49 silver badges96 bronze badges 8
  • @Shafizadeh yup one string per line, i thought i would put 4 examples to illustrate – Yehia A.Salam Commented Apr 15, 2016 at 21:51
  • Ok, what language are you using? – Shafizadeh Commented Apr 15, 2016 at 21:52
  • @Shafizadeh Javascript – Yehia A.Salam Commented Apr 15, 2016 at 21:52
  • @Shafizadeh updated the question to clarify it a bit – Yehia A.Salam Commented Apr 15, 2016 at 21:54
  • I'm not sure I get you right, do you want something like this: regex101./r/pF1nY9/3 – Shafizadeh Commented Apr 15, 2016 at 21:57
 |  Show 3 more ments

2 Answers 2

Reset to default 2

Try this:

/(([\w]+\/){3})([^\/]+)(\/.+)/gm

Regex101 Demo


var re = /(([\w]+\/){3})([^\/]+)(\/.+)/gm; 
var str = 'res/icons/android/quickshop/icon-36-ldpi.png';
var replaceWord = 'homecenter'
var subst = '$1' + replaceWord + '$4';

var result = str.replace(re, subst);
// show result
window.alert(result);

could even be written as a function:

function replaceURL(url, strReplace){
    let re = /(([\w]+\/){3})([^\/]+)(\/.+)/gm; 
    var subst = '$1' + strReplace + '$4';
    return url.replace(re, subst);
}

var originalURL = 'res/icons/android/quickshop/icon-36-ldpi.png';
var replaceWord = 'homepage';

var newURL = replaceURL(originalURL, replaceWord);

document.write(newURL);

Try this:

var str    = "res/icons/android/quickshop/icon-36-ldpi.png\nres/icons/android/quickshop/icon-48-mdpi.png\nres/icons/ios/quickshop/icon-72-hdpi.png\nres/icons/ios/quickshop/icon-96-xhdpi.png",
    result = str.replace(/([\w.-]+)(\/[\w.-]+)$/gm,"homecenter$2");
console.log(result);
alert(result);

I'm trying to replace a part of the url with another string in javascript, so for example i have the following:

res/icons/android/quickshop/icon-36-ldpi.png
res/icons/android/quickshop/icon-48-mdpi.png
res/icons/ios/quickshop/icon-72-hdpi.png
res/icons/ios/quickshop/icon-96-xhdpi.png

and I want to replace it to the following (quickshop is dynamic and can be any series of characters, mostly [a-z][A-Z])

res/icons/android/homecenter/icon-36-ldpi.png
res/icons/android/homecenter/icon-48-mdpi.png
res/icons/ios/homecenter/icon-72-hdpi.png
res/icons/ios/homecenter/icon-96-xhdpi.png

I was always very lousy when it es to regular expressions, anyone could help?

I'm trying to replace a part of the url with another string in javascript, so for example i have the following:

res/icons/android/quickshop/icon-36-ldpi.png
res/icons/android/quickshop/icon-48-mdpi.png
res/icons/ios/quickshop/icon-72-hdpi.png
res/icons/ios/quickshop/icon-96-xhdpi.png

and I want to replace it to the following (quickshop is dynamic and can be any series of characters, mostly [a-z][A-Z])

res/icons/android/homecenter/icon-36-ldpi.png
res/icons/android/homecenter/icon-48-mdpi.png
res/icons/ios/homecenter/icon-72-hdpi.png
res/icons/ios/homecenter/icon-96-xhdpi.png

I was always very lousy when it es to regular expressions, anyone could help?

Share Improve this question edited Apr 15, 2016 at 21:57 Andrea Corbellini 17.8k3 gold badges58 silver badges71 bronze badges asked Apr 15, 2016 at 21:49 Yehia A.SalamYehia A.Salam 2,0289 gold badges49 silver badges96 bronze badges 8
  • @Shafizadeh yup one string per line, i thought i would put 4 examples to illustrate – Yehia A.Salam Commented Apr 15, 2016 at 21:51
  • Ok, what language are you using? – Shafizadeh Commented Apr 15, 2016 at 21:52
  • @Shafizadeh Javascript – Yehia A.Salam Commented Apr 15, 2016 at 21:52
  • @Shafizadeh updated the question to clarify it a bit – Yehia A.Salam Commented Apr 15, 2016 at 21:54
  • I'm not sure I get you right, do you want something like this: regex101./r/pF1nY9/3 – Shafizadeh Commented Apr 15, 2016 at 21:57
 |  Show 3 more ments

2 Answers 2

Reset to default 2

Try this:

/(([\w]+\/){3})([^\/]+)(\/.+)/gm

Regex101 Demo


var re = /(([\w]+\/){3})([^\/]+)(\/.+)/gm; 
var str = 'res/icons/android/quickshop/icon-36-ldpi.png';
var replaceWord = 'homecenter'
var subst = '$1' + replaceWord + '$4';

var result = str.replace(re, subst);
// show result
window.alert(result);

could even be written as a function:

function replaceURL(url, strReplace){
    let re = /(([\w]+\/){3})([^\/]+)(\/.+)/gm; 
    var subst = '$1' + strReplace + '$4';
    return url.replace(re, subst);
}

var originalURL = 'res/icons/android/quickshop/icon-36-ldpi.png';
var replaceWord = 'homepage';

var newURL = replaceURL(originalURL, replaceWord);

document.write(newURL);

Try this:

var str    = "res/icons/android/quickshop/icon-36-ldpi.png\nres/icons/android/quickshop/icon-48-mdpi.png\nres/icons/ios/quickshop/icon-72-hdpi.png\nres/icons/ios/quickshop/icon-96-xhdpi.png",
    result = str.replace(/([\w.-]+)(\/[\w.-]+)$/gm,"homecenter$2");
console.log(result);
alert(result);

本文标签: Replace part of URL using Regex JavascriptStack Overflow