Javascript Value Replace for Multiple Values

江枫思渺然 提交于 2020-01-15 05:53:11

问题


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');

回答1:


You could chain it simply.

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


来源:https://stackoverflow.com/questions/9073372/javascript-value-replace-for-multiple-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!