How to Use the replace method and regular expression in Javascript

Source: Internet
Author: User

CopyCode The Code is 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 ");
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:

<textarea id="runcode94158"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

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:

<textarea id="runcode85872"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

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.

<textarea id="runcode63846"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

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


[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

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.


[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

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

<textarea id="runcode67050"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

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 following code.

<textarea id="runcode93754"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

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.

<textarea id="runcode13644"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

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

<textarea id="runcode80531"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

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.