从 url Jquery 中删除特定字符串
Remove particular string from url Jquery
我需要从 url 中删除尺寸,即图像路径的分辨率。举个例子:
http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg
我需要删除 -1024x682
http://raudevlocal.com/wp/wp-content/uploads/2017/05/rohit-300x118.jpg
需要删除 -300x118
您可以使用replace
函数
window.location.href.replace('-1024x682', '');
如果您想将新的用作当前的url,请尝试
window.location.href = window.location.href.replace('-1024x682', '');
使用正则表达式模式
$(document).ready(function(){
window.location.href = window.location.href.replace(/(-\d{2-6}x\d{2-6})/g,'');
});
使用正则表达式
var string = "http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg";
console.log(string.replace(/-\d+x\d+/g,''));
window.loaction.href= window.location.href.replace(/-\d+x\d+/g,'');
str = "http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg";
str.replace(/-(\d+x\d+)/, "");
output will be
http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake.jpeg
我需要从 url 中删除尺寸,即图像路径的分辨率。举个例子:
http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg
我需要删除 -1024x682
http://raudevlocal.com/wp/wp-content/uploads/2017/05/rohit-300x118.jpg
需要删除 -300x118
您可以使用replace
函数
window.location.href.replace('-1024x682', '');
如果您想将新的用作当前的url,请尝试
window.location.href = window.location.href.replace('-1024x682', '');
使用正则表达式模式
$(document).ready(function(){
window.location.href = window.location.href.replace(/(-\d{2-6}x\d{2-6})/g,'');
});
使用正则表达式
var string = "http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg";
console.log(string.replace(/-\d+x\d+/g,''));
window.loaction.href= window.location.href.replace(/-\d+x\d+/g,'');
str = "http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg";
str.replace(/-(\d+x\d+)/, "");
output will be
http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake.jpeg