2012年5月26日 星期六

【JavaScript】replace all

<script type="text/javascript">
var visitorName = "Chuck";
var myOldString = "Hello username! I hope you enjoy your stay username.";
var myNewString = myOldString.replace(/username/g, visitorName);

document.write("Old string = " + myOldString);
document.write("<br />New string = " + myNewString);

</script>


Will output as below



Old string = Hello username! I hope you enjoy your stay username.


New string = Hello Chuck! I hope you enjoy your stay Chuck.

沒有留言:

張貼留言