Hi Tomas,
In order to replace any portion of a string using JavaScript you would use the JavaScript replace method that is included in the JavaScript String object.
This method serves to be a very useful one as I myself have used it countless amounts of times.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
replace()
Replaces specified character with other characters in a string.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
To demonstrate how it works first we need to create our string that we will be manipulating.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
var strDemo = "I am now learning how to replace characters in a string using JavaScript.";
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*Note: String replace methods by default will replace only the first instance of the matching string. To replace all instances you will need to create a loop to go through the string until all instances are replaced.
To use the replace() method you will need to identify:
1. what will be replaced as well
2. what it will be replaced with.
Below you will find the syntax.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
var strDemoReplaced = strDemo.replace( "What will be replaced", "What it will be replaced with" );
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Here is an actual example:
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
var strDemo = "I am now learning how to replace characters in a string using JavaScript.";
var strDemoReplaced = strDemo.replace( "JavaScript", "PHP :-)" );