0

I want a javascript function to replace all the b's with an html <br /> tag but it's only printing <br>. Here is the function:

var destination = source.replace(/b/g,"<br \/>");

It doesn't work, is it wrong? If so, could someone please show me how to do it?

4

1 回答 1

3

原来的

没有理由\在字符串中。

var destination = source.replace(/b/g,"<br/>");

jsFiddle 示例

编辑

现在你用这段代码给出了一个示例小提琴

function gettext(){
    var input = document.getElementById("input").value;
    var value = input.replace("/b/g","<br/>");
    var output = document.getElementById("out").value = value;
}

查看替换行

var value = input.replace("/b/g","<br/>");

它是一个字符串而不是正则表达式

"/b/g"

它需要是

var value = input.replace(/b/g,"<br/>");

带有您的代码的 jsFiddle 示例

于 2012-05-17T19:00:36.030 回答