Replace () The simplest is the ability to be a simple character replacement. 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]
I think we can see the result when we run it, it only replaces the first letter. But if you add regular expressions, the results are different! Oh, yes. Replace () supports regular expressions, which match characters or strings according to the rules of regular expressions, and then give replacements!
<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]
Oh, you must have found out. This only replaces the first letter A. If you are familiar with the regular, then this will be difficult for you. Just a little change is OK.
<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]
You can also do this, see the effect!
<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]
The examples I have here are simple applications where replace () is proportional to your ability to use regular expressions at this point. Your regular expression is stronger, hehe, then you will be madly in love with it.
Of course, the reason I recommend replace () is not because it works with regular expressions, but because it can work with functions to play a powerful role.
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 Go" OD 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 selected note: If you need to introduce external JS need to refresh to perform]