JSFiddle - React, Tailwind, and code Playground

HTML

<textarea id="t">hello hello hello hello hello hello</textarea>
<a href="#" id="DoIt">BB me!</a>

JavaScript

function getSelected()
{
    var editor = $('#t');
    
    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)];
}

$('#DoIt').click(function(e) {
    e.preventDefault();
    var bits = getSelected();
    var bb = ['[b]', '[/b]'];
    $('#t').val(bits[0] + bb[0] + bits[2] + bb[1] + bits[1]);
});