Sunday, May 1, 2011

JavaScript Replace All

javascript tutorial
In this tiny tutorial we learn how we can see how we can replace all instances of a string in a String with a new string using javascript/The JavaScript function for string replace replaces the only first occurrence in the string. The function is similar to the PHP function str_replace and takes two simple parameters.The first parameter is the pattern to find and the second one is the string to replace the pattern with when found. The javascript function does not Replace All...

to replace all occurence of a string we use Regex's /g switch now our replace all function will be
function replaceAll(string, replace, otherString) {
  return string.replace(new RegExp(replace, 'g'),otherString);
}


0 comments:

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems