Question: I want to replace second space occurrence of the sentence with a br.

I have tried this but it is deleting the rest.

```
var title = "My Title Needs Brace".split(" ").slice(0, 2).join(" ");

```

Answer: 
Try the following:

```js
var title = "My Title Needs Brace".split(" ");

title.forEach(function(item, i, title){
  if(i==1)
    title[i] += "<br/>";
  else
  title[i] += ' ';
})

console.log(title.join(''));
```
You can do this without splitting the string:

```
var title = 'My Title Needs Brace'.replace(/( .*?) /, '$1<br>');

```

Here, [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) takes a [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) and a string as arguments. The regex matches everything from the first space up through the second space, keeping everything except the second space in a [capturing group](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references). The string [replaces the entire match with the contents of the capturing group](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter), followed by `'<br>'`. Since the capturing group doesn't include the second space, this effectively only replaces the second space.