The JS character replaces replace () function usage detailed explanation

Source: Internet
Author: User

The parameter replacement of the replace () method can be a function rather than a string. In this case, each match calls the function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next argument is a string that matches the subexpression in the pattern, and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself

Way one, the simplest common way

The code is as follows Copy Code

<script language= "JavaScript" >
var stringobj= "End ancient People's Republic, end ancient people";

Replace the spelling "end ancient" as "China"
and returns the replaced new character
The value of the original string stringobj has not changed
var newstr=stringobj.replace ("End Ancient", "China");
alert (NEWSTR);
</script>

Method two, using the callback function of the fixed parameter

The code is as follows Copy Code

var rep2=url.replace (reg,function (M,P1,P2,P3) {return p1+ "showbook.aspx?bookid=" +p3+ "&chapterid=" +p3});
alert (REP2);

Method III, using a callback function with a non fixed parameter

The code is as follows Copy Code

var rep3=url.replace (Reg,function () {var args=arguments; return args[1]+ showbook.aspx?bookid= "+args[2]+" & Chapterid= "+args[3];}";
alert (REP3);

Method Four, regular mode substitution

The code is as follows Copy Code


Convert Word-word to Wordword
function Camelize (s) {
Return S.replace (/-(w)/g, function (Strmatch, p1) {
return P1.touppercas ();
});
}

If

There are N of N times a typo, is not also to perform N-second side replace method to replace the typo?? Oh, don't be afraid, there is a regular expression after the use of a typo to perform a replace method. After the program has been improved, the following code

JS Code

The code is as follows Copy Code
<script language= "JavaScript" >
var reg=new RegExp ("End Ancient", "G"); Create a regular RegExp object
var stringobj= "End ancient People's Republic, end ancient people";
var newstr=stringobj.replace (Reg, "China");
alert (NEWSTR);
</script>

Analysis of problems in the process of use

The code is as follows Copy Code
Str=str.replace ("Hand", "hand.gif");

Output: Hand.gif hand Hand

Replace only once ... :(

So write

The code is as follows Copy Code
str = str.replace (/hand/, "Hand.gif")

Invalid...
All replacements should be G,

The code is as follows Copy Code
str = str.replace (/hand/g, "Hand.gif")

Or not: (

Later Baidu, Google found the result is the original to use () enclosed, will replace () in the Dongdong. The correct wording is as follows:

The code is as follows Copy Code

str = "Hand hand hand";
Str=str.replace (/(hand)/g, "hand.gif");
document.write (str);
Correct output: Hand.gif hand.gif hand.gif.

JS is another way of writing is the use of regexp:

  code is as follows copy code
equivalent to:
Reg = new RegExp ("(Hand)", "G");
   str = str.replace (reg, ' hand.gif ');

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.