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 , i'd code:
var withoutnewlines = document.getelementbyid("tainput").value.replace(/ /g, " ");
if want change both in 1 statement, i'd code:
var withoutnewlines = document.getelementbyid("tainput").value.replace(/\n/g, "<br>").replace(/ /g, " ");
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
Post a Comment