[英]Javascript wrap text with tags bbcode?
is there anyway to wrap textarea text with tags ? 反正有标签包装textarea文本吗? Javascript/Jquery 使用Javascript / jQuery的
B = Bold I = Italic U = Underline S = Strike B =大胆I =斜体U =下划线S =打击
For example: 例如:
-+-+-+-+-
B I U S
-+-+-+-+-
<textarea>
Some text here
</textarea>
When i highlight "here" and then click on bold its will be smth like this. 当我突出显示“这里”,然后单击粗体时,它将像这样。
-+-+-+-+-
B I U S
-+-+-+-+-
<textarea>
Some text [b]here[/b]
</textarea>
Hope i will find solution and this is it thanks. 希望我能找到解决方案,就是这样。
this is the solution 这是解决方案
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));
}
useage 使用率
console.log(parseBB('[b]hello [/b][u]world[/u]'));
console.log(parseBB(document.getElementById('textareaID').value));
console.log(parseBB($('#textareaID').val()));
test case 测试用例
$('#output-container').html(parseBB($('#textareaID').val()));
.. ..
function parseBB(string){
var _string = string.replace(/\n/g, '<br>'),
parseExp = new RegExp(/^(.*)\[(b|u|i|s)\]([A-Za-z0-9 ._-]+)\[\/[a-z]+\](.*)$/g);
(function run(){
if(parseExp.test(_string)){
_string = _string.replace(parseExp , '$1<$2>$3</$2>$4');
run();
}
})();
return _string;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.