Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Value Replace for Multiple Values

I am trying to replace multiple values in a string with JS replace(). The values that I want to replace include line breaks, &, #, etc... I know how to replace one value:

var string = document.getElementById('string').value.replace(\/n/g, '<br>');

However, what is the syntax to include other values. For example, how can I make the below replace functions one function?

var string = document.getElementById('string').value.replace(\/n/g, '<br>')
var string = document.getElementById('string').value.replace('&', '%26');
like image 451
user175328 Avatar asked Aug 22 '26 14:08

user175328


1 Answers

You could chain it simply.

var string = document.getElementById('string').value.replace(/\n/g, '<br>').replace('&', '%26');
like image 144
xdazz Avatar answered Aug 24 '26 05:08

xdazz