用 url 中的字符串替换域扩展名
replace a domain extension with a string in url
我有如下字符串
https://static.example.com/uploads/image-85b2-27ee598edd99-professional-clients-jpg
我要的是把.com/
换成.com/resize/100x/uploads/image-85b2-27ee598edd99-professional-clients-jpg
.
如你所见,url的其余部分是相同的我只是在.com
之后添加了/resize/100x/
。
现在这些链接可以带有任何域扩展名,例如 .io、.com、.app、.net、.gov 等。
我需要适用于所有这些的东西。我有以下解决方案,但它仅适用于 .io 和 .com。如果我继续这样做,那么下面的功能很容易变得混乱。知道如何通过 RegExp 实现吗?
const addStr = (url) => {
if (url.includes('io')) {
return url.replace('.io', '.io/resize/100x');
}
if (url.includes('com')) {
return url.replace('.com', '.com/resize/100x');
}
}
匹配任何 url 直到第一个 /
并附加你的 /resize/100x/
const addStr = (url) => {
return url.replace(/(https?:\/\/.*?)\//, '/resize/100x/');
}
console.log(addStr('http://static.example.com/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
console.log(addStr('https://static.example.io/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
console.log(addStr('https://static.example.co.uk/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
或使用URL api
const addStr = (urlStr) => {
let url = new URL(urlStr);
url.pathname = '/resize/100x' + url.pathname;
return url.toString();
}
console.log(addStr('http://static.example.com/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
console.log(addStr('https://static.example.io/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
console.log(addStr('https://static.example.co.uk/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
console.log(addStr('http://localhost/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
我有如下字符串
https://static.example.com/uploads/image-85b2-27ee598edd99-professional-clients-jpg
我要的是把.com/
换成.com/resize/100x/uploads/image-85b2-27ee598edd99-professional-clients-jpg
.
如你所见,url的其余部分是相同的我只是在.com
之后添加了/resize/100x/
。
现在这些链接可以带有任何域扩展名,例如 .io、.com、.app、.net、.gov 等。
我需要适用于所有这些的东西。我有以下解决方案,但它仅适用于 .io 和 .com。如果我继续这样做,那么下面的功能很容易变得混乱。知道如何通过 RegExp 实现吗?
const addStr = (url) => {
if (url.includes('io')) {
return url.replace('.io', '.io/resize/100x');
}
if (url.includes('com')) {
return url.replace('.com', '.com/resize/100x');
}
}
匹配任何 url 直到第一个 /
并附加你的 /resize/100x/
const addStr = (url) => {
return url.replace(/(https?:\/\/.*?)\//, '/resize/100x/');
}
console.log(addStr('http://static.example.com/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
console.log(addStr('https://static.example.io/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
console.log(addStr('https://static.example.co.uk/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
或使用URL api
const addStr = (urlStr) => {
let url = new URL(urlStr);
url.pathname = '/resize/100x' + url.pathname;
return url.toString();
}
console.log(addStr('http://static.example.com/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
console.log(addStr('https://static.example.io/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
console.log(addStr('https://static.example.co.uk/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));
console.log(addStr('http://localhost/uploads/image-85b2-27ee598edd99-professional-clients-jpg'));