Javascript: Using regEx to change more than one character or group of characters at a time -


i know how code simple regular expressions, question changing more 1 character or characters @ time - not more 1 instance of character or characters, more 1 match characters or characters separate values in 1 call replace.

for instance, if want change newline characters <br> in text textarea, might code:

var withoutnewlines = document.getelementbyid("tainput").value.replace(/\n/g, "<br>"); 

if want change spaces &nbsp;, i'd code:

var withoutnewlines = document.getelementbyid("tainput").value.replace(/ /g, "&nbsp;"); 

if want change both in 1 statement, i'd code:

var withoutnewlines = document.getelementbyid("tainput").value.replace(/\n/g, "<br>").replace(/ /g, "&nbsp;"); 

my question is:

is there way code 1 regular expression used make both changes 1 call replace()?

it's little unnecessary, try:

var newstr = "some string".replace(/o|m/g, function (match) {     if (match === "o") {         return "i";     } else if (match === "m") {         return "r";     } }); console.log(newstr); 

where replacing "o" "i", , "m" "r".

reference:


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -