Javascript Replace new Regexp

Source: Internet
Author: User

I have nothing to do today. I will explain the replace method in Javascript. It is reasonable to say something wrong or unreasonable, because I am neither an old bird nor a newbie, I also don't know what the birds are ?? Ah ~~

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.

<Script language = "JavaScript">
VaR stringobj = "People's Republic Of fingu, people of fingu ";

// Replace the incorrect word "final" with "China"
// Return the new character after replacement.
// The value of the original string stringobj has not changed
VaR newstr = stringobj. Replace ("final ancient", "China ");
Alert (newstr );
</SCRIPT>

You are smarter than me. After reading the above example, you will find that the second incorrect word "final ancient" is not replaced with "China ", we can execute the secondary replace method to replace the second error word "final,ProgramThe improvements are as follows:

<Script language = "JavaScript">
VaR stringobj = "People's Republic Of fingu, people of fingu ";

// Replace the incorrect word "final" with "China"
// Return the new character after replacement.
// The value of the original string stringobj has not changed
VaR newstr = stringobj. Replace ("final ancient", "China ");

Newstr = newstr. Replace ("final ancient", "China ");
Alert (newstr );
</SCRIPT>

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 .. After the program is improvedCodeAs follows:

<Script language = "JavaScript">
VaR Reg = new Regexp ("final ancient", "G"); // create a regular Regexp object
VaR stringobj = "People's Republic Of fingu, people of fingu ";
VaR newstr = stringobj. Replace (Reg, "China ");
Alert (newstr );
</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.

<Script language = "JavaScript">
VaR STR = "People's Republic of China, People's Republic of China ";
VaR newstr = Str. Replace (/(person)/g, "<font color = Red> $1 </font> ");
Document. Write (newstr );
</SCRIPT>

The above program lacks interaction, so we can improve the program so that we can enter the characters to search.

<Script language = "JavaScript">
VaR S = prompt ("Enter the characters to be searched", "persons ");
VaR Reg = new Regexp ("(" + S + ")", "G ");
VaR STR = "People's Republic of China, People's Republic of China ";
VaR newstr = Str. Replace (Reg, "<font color = Red> $1 </font> ");
Document. Write (newstr );
</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.

<Script language = "JavaScript">
Function Test ()
{
Alert ("parameter count:" + arguments. Length );
Alert ("value of each parameter:" + arguments [0]);
Alert ("value of the second parameter" + arguments [1]);
// All parameters can be read through the for loop.
}

Test ("AA", "BB", "CC ");
</SCRIPT>

After reading the above program, let's look at the following interesting program.

<Script language = "JavaScript">
VaR Reg = new Regexp ("\ D", "g ");
VaR STR = "abd1afa4sdf ";
Str. Replace (Reg, function () {alert (arguments. Length );});
</SCRIPT>

We were surprised to find that the anonymous function was actually executed twice, and there were three parameters in the function. Why would it be 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 code below.

<Script language = "JavaScript">
Function Test ()
{
For (VAR I = 0; I <arguments. length; I ++)
{
Alert ("th" + (I + 1) + "parameter value:" + arguments [I]);
}

}
VaR Reg = new Regexp ("\ D", "g ");
VaR STR = "abd1afa4sdf ";
Str. Replace (Reg, test );
</SCRIPT>

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 understanding these problems, we can use another method.

<Script language = "JavaScript">
Function Test ($1)
{
Return "<font color = 'red'>" + $1 + "</font>"
}
VaR S = prompt ("Enter the characters to be searched", "persons ");
VaR Reg = new Regexp ("(" + S + ")", "G ");
VaR STR = "People's Republic of China, People's Republic of China ";
VaR newstr = Str. Replace (Reg, test );
Document. Write (newstr );
</SCRIPT>

After reading the above program, you can use the matching characters as needed. The following is an example of an application.

<Script language = "JavaScript">
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"
Function Test ($1)
{
VaR gyear = (new date (). getyear ()-parseint ($1) + 1;
Return $1 + "(" + gyear + "Year of birth )";
}
VaR Reg = new Regexp ("(+) years", "g ");
VaR newstr = Str. Replace (Reg, test );
Alert (STR );
Alert (newstr );
</SCRIPT>

This article from: Example tutorial Network (http://www.shilicn.com/) original link: http://www.shilicn.com/cxyuyan/javascrips/3066.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.