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:

http://jsfiddle.net/a2lkd/6/

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

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -