Question: I have uploaded a data set into R. The dataset has 2 columns

```
user_id merchant_id
514729  14852,16695
1240327 23590
7457    211
359027  2483
463149  5802
514730  5460,1896
41953   7183,147105
927805  304,3909,4151,32,3,39171

```

As you can see that some user ids are associated with multiple merchants. What I am looking to do is transform the data in such a way that
I have the following schema

```
User Id MerchantId1 MerchantId2 MerchantId3 MerchantId 4
123445  0           1           0           1
123453  1           0           0           0

```

Basically I want to create a matrix of userid and merchant ids with a 1 or 0 based on if a user\_id has a merchant\_id or not.

Any suggestions/help on how I can accomplish this?

I am looking to use this to build a recommendation system. Any help would be great.

Answer: 
The easiest way is the three-step process:

```
text = text.
  replace(/<br><\/br><br><\/br>/g, '____MAGICMARKER____').
  replace(/<br><\/br>/g, '').
  replace(/____MAGICMARKER____/g, '<br></br><br></br>')

```

hoping that `____MAGICMARKER____` is never, nor will ever, be a part of your text. Another possibility (and a better, foolproof one) would be:

```
text = text.replace(/(<br><\/br>)+/g, function(all, once) {
  if (all == once) return "";
  return all;
});

```

Note that this is all written assuming your premise in the question is correct: you have a text (i.e. a string). If you have a document fragment, then you want to do this operation on its `innerHTML`.
Try utilizing `html` , `RegExp` `/<br><br>+[\s|\n]/g` to match `<br><br>` followed by space character or new line character

```js
$("body").html(function(i, html) {
  return html.replace(/<br><br>+[\s|\n]/g, "")
});

console.log($("body").html());
```

```html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<body>
<br></br>
and several double line breaks like so

<br></br><br></br>
<br></br>
and several double line breaks like so

<br></br><br></br>
<br></br>
and several double line breaks like so

<br></br><br></br>
</body>
```