Question: Can anyone who has worked on Android multimedia framework tell me what's the use AwesomePlayer in Android multimedia framework?

Answer: 
Use the [selectionStart](http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectionstart%28v=vs.110%29.aspx) and [selectionEnd](http://help.dottoro.com/ljxjdqwh.php) properties and add your BBcode on either end.

Once you've got the selected text, get the prepending text by getting a substr from the start of the textarea contents until the selectionStart point, and the appended text after the selection from selectionEnd to the end of the textarea contents.

Then set the value of the textarea like this (psuedo): 

```
$('textarea').val( prependingText + BBCodeStartTag + selectedText + BBCodeEndTag + endText );
// e.g. :          You what,        [b]              you what       [/b]           ...

```

This method is really the only way to do what you are proposing, because it targets the exact section of text that you have selected rather than doing a `replace()` on the matching text which as you mention will replace the first occurrence instead of what you've selected. In a block of text like `hello hello hello hello hello` if you select the fourth `hello` your code would replace the first `hello` instead...

**Edit:** [here's an example](http://jsfiddle.net/kv5Yv/) using a [function from this answer](https://stackoverflow.com/a/8549862/2812842) to illustrate this idea:

```
function getSelected() {
    var editor = $('textarea');

    var u     = editor.val();
    var start = editor.get(0).selectionStart;
    var end   = editor.get(0).selectionEnd;

    return [u.substring(0, start), u.substring(end), u.substring(start, end)];
}

$('#ButtonToAddBBCode').click(function(e) {
    e.preventDefault();
    var bits = getSelected();
    var bb = ['[b]', '[/b]']; // your example BB code
    $('textarea').val(bits[0] + bb[0] + bits[2] + bb[1] + bits[1]);
});

```
You *definitely* want to look at [Rangy](https://code.google.com/p/rangy/) for that. Using a straight up `replace` is not robust enough. The [CSS Class Applier Module](https://code.google.com/p/rangy/wiki/CSSClassApplierModule) does pretty much exactly what you want. Tim Down is on here somewhere, if you add Rangy as a tag in your question he'll probably comment...