Grammar
Stringobject.replace (regexp/substr,replacement)
Replace
Used to find a string that matches a regular expression, and then use a new string instead of a matching string.
| The code is as follows |
Copy Code |
var result1 = A.replace (Re, "Hello"); RESULT1 = "Hello" var result2 = B.replace (Re, "Hello"); RESULT2 = ", World" |
Example 1
| The code is as follows |
Copy Code |
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; Here I want to replace the letter A with the letter A Alert (Strm.replace ("A", "a")); </script> |
The substitution above is simple, and we say that replace can be replaced with a regular match.
Example 2
| The code is as follows |
Copy Code |
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; Replace the letter a all with the letter A Alert (Strm.replace (/a/g, "a")); </script> |
Replace all characters to replace
Replace the letter I all with 5
| code is as follows |
copy code |
| <script language= "javascript" var txt = "Sjfisjfisdjfijsidfjioalfjewofjjgs"; Alert (txt. replace (/i/g, "5")); </script> ///replace only the first letter I with 5 <script language= "javascript" var txt = " Sjfisjfisdjfijsidfjioalfjewofjjgs "; Alert (txt. replace ("I", "5")); </script> |