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.