Detailed description of the replace method in the javascript Regular Expression
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 ..
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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<! DOCTYPE> <Html> <Head> <Meta charset = 'utf-8'> <Title> </title> </Head> <Script type = "text/javascript"> Window. onload = function (){ Var oTxtarea = document. getElementsByTagName ('textarea '); Var oInpt = document. getElementById ('bt '); Var re =/your sister | fuck | your uncle | Meng/g; OTxtarea [0]. value = 'I want to watch your sister, no, you big master, I want to sprout your sister, no, you think too much '; OInpt. onclick = function (){ OTxtarea [1]. value = oTxtarea [0]. value. replace (re ,'*'); }; }; </Script> <Body> <Textarea rows = '7' cols = '20'> </Textarea> <br/> <Input id = 'bt 'Type = 'button 'value = 'converted to an uncivilized language'> <br/> <Textarea rows = '7' cols = '20'> </Textarea> <br/> </Body> </Html> |
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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<! DOCTYPE> <Html> <Head> <Meta charset = 'utf-8'> <Title> </title> </Head> <Script type = "text/javascript"> Window. onload = function (){ Var oTxtarea = document. getElementsByTagName ('textarea '); Var oInpt = document. getElementById ('bt '); Var re =/your sister | fuck | your uncle | Meng/g; OTxtarea [0]. value = 'I want to watch your sister, no, you big master, I want to sprout your sister, no, you think too much '; OInpt. onclick = function (){ OTxtarea [1]. value = oTxtarea [0]. value. replace (re, function (obj ){ Alert (obj ); /* Alert (obj. length );*/ }); }; }; </Script> <Body> <Textarea rows = '7' cols = '20'> </Textarea> <br/> <Input id = 'bt 'Type = 'button 'value = 'converted to an uncivilized language'> <br/> <Textarea rows = '7' cols = '20'> </Textarea> <br/> </Body> </Html> |
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 *
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<! DOCTYPE> <Html> <Head> <Meta charset = 'utf-8'> <Title> </title> </Head> <Script type = "text/javascript"> Window. onload = function (){ Var oTxtarea = document. getElementsByTagName ('textarea '); Var oInpt = document. getElementById ('bt '); Var re =/your sister | fuck | your uncle | Meng/g; OTxtarea [0]. value = 'I want to watch your sister, no, you big master, I want to sprout your sister, no, you think too much '; OInpt. onclick = function (){ OTxtarea [1]. value = oTxtarea [0]. value. replace (re, function (obj ){ Var a = ''; For (var I = 0; I <obj. length; I ++ ){ A + = '*'; } Return; }); }; }; </Script> <Body> <Textarea rows = '7' cols = '20'> </Textarea> <br/> <Input id = 'bt 'Type = 'button 'value = 'converted to an uncivilized language'> <br/> <Textarea rows = '7' cols = '20'> </Textarea> <br/> </Body> </Html> |
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.