regex - Javascript: is this the right way to use a regular expression to replace multiple parts of a string? -
i curious regular expressions and, being new programming wondering if there problems (or better way) of replacing different parts of same string in fashion:
str2.replace(/hanna/ig, 'clark').replace(/anna/ig, 'ark')
for example:
html:
<p id="test2" onclick="fixit2()">hanna hanna bobanna banana nana fo fanna</p>
javascript:
function fixit2() { var str2 = document.getelementbyid('test2').innerhtml; alert(str2); alert(str2.replace(/hanna/ig, 'clark').replace(/anna/ig, 'ark')); }
i have been working on here:
well, if achieves want there no problem... using regular expressions replace patterns in strings common practice. should make sure regular expression using right 1 task. please note strings immutable , new string returned replace
operations. original string not modified.
Comments
Post a Comment