Js forward slash does not print

I need a javascript function to replace all b with an html tag <br /> , but only print <br> . Here is the function:

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

It doesn't work, does it? If yes, can someone please show me how to do this?

0
source share
1 answer

Original

There is no reason for \ .

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

JsFiddle example

Edit

Now you have provided a sample script with this code

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

Look at the replacement string

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

this is a non-regex string

 "/b/g" 

It should be

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

JsFiddle example with code

+3
source

Source: https://habr.com/ru/post/1413052/


All Articles