我有一个字符串
var urlString = $('#link1').attr('src');
但是,它看起来像这样://mysite.com/image1_medium.jpeg?v = 1233301923
我想删除一些文字,以便urlString ==' // mysite.com/image1.jpeg'
答案 0 :(得分:3)
regular expression使用https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
var url = "//mysite.com/image1_medium.jpeg?v=1233301923";
var newUrl = url.replace(/(.*)_medium(.*)\?.*/, "$1$2")
答案 1 :(得分:-1)
var new_url = old_url.substring(0, old_url.indexOf('?')).replace("_medium", "");