Detailed description of the replace method in the javascript Regular Expression, and the regular expression replace

Source: Internet
Author: User

Detailed description of the replace method in the javascript Regular Expression, and the regular expression replace

In the previous article, I have introduced four basic methods of regular expressions, and I also mentioned the replace method.

Let's review the use of the replace method:
First define a regular object: var re =/conditions for intermediate write matching /;
Replace (): Regular Expression matching string. If the match is successful, replace the matched string with a new one.
Syntax: String. replace (re, new string );

For example, it is often seen on the Internet that uncivilized words will be replaced by *. Let's give it a try:

<! DOCTYPE> 

Of course, we are not satisfied with the above conversion effect. What I want to achieve is to display a few * numbers after converting a few words.

At this time, we need to analyze it. In fact, parameter 2 in replace (parameter 1, parameter 2) can be a callback function. We will modify the program, replace the second parameter with the callback function and pass a parameter to the callback function.

<! DOCTYPE> 

It can be seen that the above result is very strange. The second parameter is the callback function, but when the parameters in the callback function are displayed, they are all successfully matched strings.

Then we can process each result in this parameter, and several characters will be generated *

<! DOCTYPE> 

Through the above example, is it a further step to understand the replace method .....

The above is all the content of this article. I hope you will like it.

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.