var main_str ="i am working as a php developer,php is favorite language ";
and you want to programmatically replace php to JAVA, you have to write the code as:
main_str = main_str.replace(/php/i,'JAVA');
Here,as you can see we calling the replace function with the regular expression /php/i - as the first parameter which mean that it will replace only first occurrence of the string "php" but not the later ones. If we want to replace all php to JAVA we have to change the expression to /php/g - this will replace all occurrence of php in the main string.