简体   繁体   English

用数组元素上的RegExp替换()

[英]replace() with RegExp on array elements

I want to write a JavaScript function which converts some simple BBcode Tags like [red] [/red] to Html-Tags. 我想编写一个JavaScript函数,将一些简单的BBcode标签(如[red] [/ red])转换为Html-Tags。 I think the replace() function is the best way to do it. 我认为replace()函数是最好的方法。 I wrote a simple testfunction to try it, but it does not seem to work. 我写了一个简单的测试功能来尝试它,但似乎没有用。

/**
* @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);
}

It should convert "[red]hello[/red]" to "<font color='red'>hello</font>" , but it just gives me a weird string. 它应该将"[red]hello[/red]""<font color='red'>hello</font>" ,但它只是给了我一个奇怪的字符串。

What is wrong? 怎么了? I think this has something to do with my regular expression. 我认为这与我的正则表达式有关。

[ and ] have special meaning in regular expressions and need to be escaped, moreover you do not need regular expressions the way you've written your code and can just do: []在正则表达式中具有特殊含义,需要进行转义,而且您不需要像编写代码那样使用正则表达式,而且可以这样做:

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++) {
        while(text.indexOf(bbTags[i])!==-1){
            text = text.replace(bbTags[i], htmlTags[i]);
        }
    }

    alert(text);
}

Just to let you know, you can use array literals so instead of arrays. 只是为了让您知道,您可以使用数组文字而不是数组。 new Array(comma seperated values) is identical to [comma seperated values] in javascript. new Array(comma seperated values)与javascript中的[comma seperated values]相同。 Also, you can use your array like a map in your case, for example: 此外,您可以在您的情况下像地图一样使用您的数组,例如:

var bbTagsToHTML = {}
bbTagsToHtml["[red]"] = "<font color='red'>"

and iterate through that. 并迭代。

If you would like you can escape your regular expressions too, please see How do you use a variable in a regular expression? 如果您希望也可以转义正则表达式,请参阅如何在正则表达式中使用变量? for a function that does that. 对于这样做的功能。

You can also do that manually. 您也可以手动执行此操作。 "[red]" becomes "\\[red\\]" (the bracket escaped). "[red]"变为"\\[red\\]" (括号转义)。

just change this line 只是改变这一行

text = text.replace(re, htmlTags[i]);

into

text = text.replace(bbTags[i], htmlTags[i]);

removing unuseful code. 删除无用的代码。

replace works also on 'normal' (not regex) values as argument. replace也可以将'normal'(非正则表达式)值作为参数。

If you want to do it with regex you can simplify a lot . 如果你想用正则表达式做,你可以简化很多 No arrays or loops: 没有数组或循环:

var str = '[red]foo[/red] hello world [blue]hey[/blue]',
    re = /\[(\w+)\](.*)\[\/\1\]/g;

str = str.replace(re, '<font color="$1">$2</font>');

console.log(str);
//^ <font color="red">foo</font> hello world <font color="blue">hey</font>

Also, as a side note, font is rarely used anymore I'd suggest you use a span with a class. 另外,作为旁注, font很少使用,我建议你使用带有类的span

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM