我想编写一个 JavaScript 函数,将一些简单的 BBcode 标签(如 [red] [/red])转换为 Html-Tags。我认为 replace() 函数是最好的方法。我写了一个简单的 testfunction 来尝试一下,但它似乎不起作用。
/**
* @function
* @description Replaces the bb-tags with html-tags
*/
function bbToHtml(form) {
debugger
var text = form.text.value;
bbTags = new Array("[red]", "[yellow]", "[green]", "[/red]", "[/yellow]", "[/green]");
htmlTags = new Array("<font color='red'>", "<font color='yellow'>", "<font color='green'>", "</font>", "<font>", "</font>");
for (var i = 0; i < bbTags.length; i++) {
var re = new RegExp(bbTags[i], "g");
text = text.replace(re, htmlTags[i]);
}
alert(text);
}
它应该转换"[red]hello[/red]"
为"<font color='red'>hello</font>"
,但它只是给了我一个奇怪的字符串。
怎么了?我认为这与我的正则表达式有关。