Question: I am retrieving some data from an external API using javascript, I'm then displaying this data on a HTML page. 

Within this returned data is a URL, it's in the following format;

```
var url = https://img.evbuc.com/moreStuff

```

I need to rewrite this URL so that it's prefixed with `www`, like this;

```
var url = https://www.img.evbuc.com/moreStuff

```

I want to achieve this using either javascript or jquery.

How can I achieve this? An explanation of the correct code would be great too.

Answer: 
If you want something that will work with any protocol, try this regex: 

```
var url = "https://img.evbuc.com/moreStuff"
var new_url = url.replace(/^([a-zA-Z][a-zA-Z0-9\.\+\-]*):\/\//, "$1://www.")
console.log('new URL: ', new_url)

```
Simple string operations:

```js
var url = 'https://img.evbuc.com/moreStuff'
var newUrl = url.split('//')[0] + '//www.' + url.split('//')[1]
console.log(newUrl)
```

and yet another way to do this is like this:

```js
var url = 'https://img.evbuc.com/moreStuff'
var newUrl = url.replace('https://', 'https://www.')
console.log(newUrl)
```