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
2 Answers
Reset to default 2Try 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
2 Answers
Reset to default 2Try 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
版权声明:本文标题:Replace part of URL using Regex Javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745572461a2156816.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论