Monday, September 10, 2012

repcale all words in javascript in single statement

To replacing all matches at once just put a "g" at the end of the regular expression.

<script type="text/javascript">
var personName = "Mahesh";
var myOldString = "Hello [name]! [name] you are selected. We have a t-shirt with your [name]";
var myNewString = myOldString.replace(/[name]/g, personName
</script>