Example of JavaScript Replace String Function

Above example will replace the first occurrence of space with hyphen "-".

Output

This-is first example

Without specifying any search criteria it will search for the first occurrence of specified character that is to be replaced and will return the string by replacing the pattern with specified string or character. As you can see above example has returned the string with replaced character from the left side. It shows that javascript starts string traversing from left to right. Next learn from other Javascript Replace String Function Examples, how to find and replace all the occurrences of specified pattern with new string pattern or characters.

Output:

You can see the output of above discussed code from the following links: