stringObject.replace([string pattern to find], [new string]);
Replace function takes 2 parameters:
- First parameter is passed as a string pattern to be searched in the provided string
- Second parameter is passed as a string of new characters to be placed.
In the first parameter you can use flags also e.g.: to search the whole string for replacing the specified characters you can use "g" and for case insensitive searching you can use "i".
var myString = new String(); myString = "This is first example"; document.write(myString.replace(/ /,"-"));
Above example will replace the first occurrence of space with hyphen "-".
This-is first example