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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -