The sample code is as follows:
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; Here I want to replace the letter A with letter a alert (Strm.replace ("A", "a")); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
It replaces only the first letter. But if you add regular expressions, the results are different! Replace () supports regular expressions, which match characters or strings according to the rules of regular expressions, and then give replacements!
Note: The replaced part does not need double quotes.
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; Here I want to replace the letter A with letter a alert (Strm.replace (/a/, "a")); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
This only replaces the first letter A.
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; Replace the letter A with the letter a alert (Strm.replace (/a/g, "a")). </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
It is clear from the above that when a regular expression has a "G" flag, the representative will process the entire string.
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; Alert (Strm.replace (/JavaScript) \s* (IS)/g, "$ fun. It $")); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Let's take a look at a simple example: capitalize all the first letters of the word.
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; function Change (word) {return Word.indexof (0). toUpperCase () +word.substring (1); Alert (Strm.replace (/\b\w+\b/g,change)); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
It is clear from the above that when a regular expression has a "G" flag, the representative will handle the entire string, that is, the transformation of the function change will be applied to all matching objects. The function has three or more parameters, depending on the regular expression.
With functions and regular expressions, the replace () processing string has an unprecedented power!
Finally, for example, it is so easy to reverse all the words in a string, using the replace () process.
<script language= "JavaScript" > var strm = "JavaScript is a good script language"; function Change (word) {var result = Word.match (/(\w)/g); if (result) {var str = ""; for (var i=result.length-1; i>=0; i--) {str = result; return str; else {return "null"; } alert (Strm.replace (/\b (\w) +\b/g,change)); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Friends who are unfamiliar with regular expressions can refer to the following article
Regular expression 30-minute introductory tutorial
Regular expression base data