Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to use a variable in replace function of JavaScript

elizaselizas Member Posts: 15
In JavaScript replace function is basically used to replace some string in an another string. For instance, if your main string is

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.
Thanks
Eliza
[code]http://www.mindfiresolutions.com/[/code]

Comments

Sign In or Register to comment.