Building off what Soufiane posted, here's the code translated to jquery with the ability to pass in the open and close tags:
function wrapText(elementID, openTag, closeTag) {
var textArea = $('#' + elementID);
var len = textArea.val().length;
var start = textArea[0].selectionStart;
var end = textArea[0].selectionEnd;
var selectedText = textArea.val().substring(start, end);
var replacement = openTag + selectedText + closeTag;
textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
}
Usage would then be like so:
wrapText("myTextArea", "[#bold]", "[/bold]");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…