我正在使用javascript从外部API检索一些数据,然后将这些数据显示在HTML页面上。
在这个返回的数据中是一个URL,它的格式如下;
var url = https://img.evbuc.com/moreStuff我需要重写这个网址,使它以www为前缀,如下所示;
var url = https://www.img.evbuc.com/moreStuff我想使用javascript或jquery来实现这一点。
我如何才能做到这一点?对正确代码的解释也会很好。
发布于 2019-07-11 16:47:41
简单的字符串操作:
var url = 'https://img.evbuc.com/moreStuff'
var newUrl = url.split('//')[0] + '//www.' + url.split('//')[1]
console.log(newUrl)
另一种方法是这样做的:
var url = 'https://img.evbuc.com/moreStuff'
var newUrl = url.replace('https://', 'https://www.')
console.log(newUrl)
https://stackoverflow.com/questions/56985060
复制相似问题