The syntax of the replace method is stringobj. replace (rgexp, replacetext) Where stringobj is a string, reexp can be a regular expression object (Regexp) or a string (string), and replacetext is a replacement string found .. For better understanding, the following is a simple example.
JSCode
1. <script language = "JavaScript">
2. var stringobj = "People's Republic Of fingu, people of fingu ";
3.
4. // Replace the incorrect word "final ancient" with "China"
5. // and return the new character after replacement
6. // the value of the original string stringobj has not changed
7. var newstr = stringobj. Replace ("final", "China ");
8. Alert (newstr );
9. </SCRIPT>
if you are smarter than me, after reading the above example, you will find that the second incorrect word "fingu" is not replaced with "China ", we can execute the secondary replace method to replace the second error word "final". After the Program is improved, it is as follows:
JS Code
1.
We can think about it carefully. If there is a n error, will the n replace method of n be executed to replace the error ?? Oh, don't worry. If you have a regular expression, you don't need to execute the replace method once .. The code after the program is improved is as follows:
JS Code
1. <script language = "JavaScript">
2. var Reg = new Regexp ("final ancient", "G"); // create a regular Regexp object
3. var stringobj = "People's Republic Of fingu, people of fingu ";
4. var newstr = stringobj. Replace (Reg, "China ");
5. Alert (newstr );
6. </SCRIPT>
The above is the simplest application of the replace method. Do you understand it ?? The following describes a slightly more complex application ..
Search on some websitesArticleWhen the search keyword is highlighted and the color is changed ?? How is this implemented ?? In fact, we can use a regular expression to implement it. How can we implement it? For the simple principle, see the following code.
JS Code
1. <script language = "JavaScript">
2. var STR = "People's Republic of China, People's Republic of China ";
3. var newstr = Str. Replace (/(person)/g, "<font color = Red> $1 </font> ");
4. Document. Write (newstr );
5. </SCRIPT>
The above program lacks interaction, so we can improve the program so that we can enter the characters to search.
JS Code
1. <script language = "JavaScript">
2. var S = prompt ("Enter the character in the search", "person ");
3. var Reg = new Regexp ("(" + S + ")", "G ");
4. var STR = "People's Republic of China, People's Republic of China ";
5. var newstr = Str. Replace (Reg, "<font color = Red> $1 </font> ");
6. Document. Write (newstr );
7. </SCRIPT>
You may not understand the meaning of the special character $1. In fact, $1 represents the characters in brackets in the left expression, that is, the first child match, similarly, $2 indicates the second child match .. What is a child match ?? In layman's terms, each brace on the left is the first word match, and the second brace is the second child match ..
How can we perform operations on the searched characters ?? Before implementation, let's talk about how to obtain the parameters of a function .. Within the function, there is an arguments set, which stores all parameters of the current function. You can obtain all parameters of the function through arguments. for your understanding, see the following code.
JS Code
1. <script language = "JavaScript">
2. Function Test (){
3. Alert ("number of parameters:" + arguments. Length );
4. Alert ("value of each parameter:" + arguments [0]);
5. Alert ("second parameter value" + arguments [1]);
6. // All parameters can be read through the for loop.
7 .}
8.
9. Test ("AA", "BB", "cc ");
10. </SCRIPT>
after reading the above program, let's look at the following interesting program
JS Code
1.
we are surprised to find that the anonymous function is actually executed twice, and there are three parameters in the function. Why is it executed twice ?? It is easy to think that because the regular expression we write matches a single number, and the detected string has exactly two digits, the anonymous function is executed twice .. What are the three parameters in an anonymous function ?? To solve this problem, let's look at the following code.
JS Code
1.
after observation, we found that the first parameter represents the matched characters, and the second parameter represents the minimum index position (Regexp. index), the third parameter indicates the matched string (Regexp. input ). In fact, the number of these parameters will increase as the sub-match increases. After clarifying these problems, we can use another method
JS Code
1.
After reading the above program, you can use the matching characters as needed. The following is an example of an application.
JS Code
1. <script language = "JavaScript">
2. var STR = "He is 22 years old, she is 20 years old, his father is 45 years old, her father is 44 years old, a total of 4 people"
3. Function Test ($1 ){
4. var gyear = (new date (). getyear ()-parseint ($1) + 1;
5. Return $1 + "(" + gyear + ")";
6 .}
7. var Reg = new Regexp ("(File: // D/+) years old", "G ");
8. var newstr = Str. Replace (Reg, test );
9. Alert (STR );
10. Alert (newstr );
11. </SCRIPT>