I'm trying to take a textarea input that takes any YouTube URL as the input and embed the video much like Facebook does. 

I have:

```
var text = $('#content').val().split(' ');
    for (i = 0; i < text.length; i++) {
        var test = text[i].indexOf('youtube.com/watch');
        var check = text[i].indexOf('[youtube=');

        if (test != -1 && check == -1) {
            var ytid = text[i].substring(text[i].lastIndexOf('=') + 1);
            text[i] = '[youtube=' + ytid + ']';
        }
    }
var boxval = text.join(' ').trim();

```

which takes any YouTube URL and makes it into a `[youtube=videoid]`. Problem then is that when a URL is submitted with a `<br>` or a `\n` at the end it adds the `]` afterwards. 

Anyone know of a better way?