如何在富文本编辑器中使用[b]而不是<;b>;使用javascript或jquery

如何在富文本编辑器中使用[b]而不是<;b>;使用javascript或jquery,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在为我的项目制作文本编辑器,我需要为论坛使用这个编辑器,我正在使用javascript制作简单的文本编辑器,一切正常。问题是我不能使用或,因为htmlentities。我的问题是,如何使用[b][/b][/code>而不是或[i][/i][/code>而不是来加粗文本,我非常需要这一点。我需要你们的帮助,谢谢。 下面是我的代码: <html> <head> <meta http-equiv="Content-Type" content="text/html; c

我正在为我的项目制作文本编辑器,我需要为论坛使用这个编辑器,我正在使用javascript制作简单的文本编辑器,一切正常。问题是我不能使用
,因为htmlentities。我的问题是,如何使用
[b][/b][/code>而不是
[i][/i][/code>而不是
来加粗文本,我非常需要这一点。我需要你们的帮助,谢谢。

下面是我的代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<style>
#my_textarea{
    width: 320px;
    height: 150px;
    border: thin solid #000;
    color: #000;
    padding: 10px;
    min-height: 150px;
    min-width: 320px;
    max-height: 150px;
    max-width: 320px;
}
#preview{
    width: 320px;
    height: 150px;
    border: thin solid #000;
    color: #000;
    padding: 10px;
}
</style>
<script type="text/javascript"> 
function formatText(tag) {
   var myTextArea = document.getElementById('my_textarea');
   var myTextAreaValue = myTextArea.value;
   var selected_txt = myTextAreaValue.substring(myTextArea.selectionStart, myTextArea.selectionEnd);
   var before_txt = myTextAreaValue.substring(0, myTextArea.selectionStart);
   var after_txt = myTextAreaValue.substring(myTextArea.selectionEnd, myTextAreaValue.length);
   myTextArea.value = before_txt + '<' + tag + '>' + selected_txt + '</' + tag + '>' + after_txt;
}
function preview() {
    var textbox , view ;
    textbox = document.getElementById('my_textarea');
    view = document.getElementById("preview");
    view.innerHTML = textbox.value
}

</script>
</head>

<body><br>
<form>
<input name="title" id="title" type="text" size="80" maxlength="80" /><br><br>
<input type="button" value="Bold" onClick="formatText ('b');" /> 
<input type="button" value="Italic" onClick="formatText ('i');" /> 
<input type="button" value="Underline" onClick="formatText ('u');" /> 
<input type="button" value="Unordered List" onClick="olist ('ul');" /><br><br>
<textarea name="my_textarea" id="my_textarea" style="width:300px;"></textarea><br>
<input type="submit" value="Submit" name="submit" id="submit" /><br><br>
</form>

<div id="preview"></div><br>
<button id="btn" onClick="preview();">Preview</button>

</body>

无标题文件
#我的工作区{
宽度:320px;
高度:150像素;
边框:薄实线#000;
颜色:#000;
填充:10px;
最小高度:150px;
最小宽度:320px;
最大高度:150像素;
最大宽度:320px;
}
#预演{
宽度:320px;
高度:150像素;
边框:薄实线#000;
颜色:#000;
填充:10px;
}
函数格式文本(标记){
var myTextArea=document.getElementById('my_textarea');
var myTextAreaValue=myTextArea.value;
var selected_txt=myTextAreaValue.substring(myTextArea.selectionStart,myTextArea.selectionEnd);
var before_txt=myTextAreaValue.substring(0,myTextArea.selectionStart);
_txt之后的变量=myTextAreaValue.substring(myTextArea.selectionEnd,myTextAreaValue.length);
myTextArea.value=在\u txt+“”+选中的\u txt+“”+在\u txt之后;
}
函数预览(){
var文本框,视图;
textbox=document.getElementById('my_textarea');
视图=document.getElementById(“预览”);
view.innerHTML=textbox.value
}









预览


演示:

我不确定这是否是您想要的,但在设置预览区域的innerHtml时,您可以使用[and]并将其替换为

view.innerHTML = textbox.value.replace(/\[/g, '<').replace(/\]/g, '>');
view.innerHTML=textbox.value.replace(/\[/g',);

请参见

您可以尝试使用正则表达式来解析BBCode-ish内容。
/\[(/)?(\w+)/
可能会起作用;请考虑将其与
替换和回调一起使用。对于预览,这并不能完全解决我的问题,但可能会给我一种解决问题的方法