JavaScript function Replace parameter flexible use __ block chain

Source: Internet
Author: User

In JavaScript, the Replace function acts as a string substitution function, which is a powerful string manipulation function for recommended use of common string operations. This essay will come to a deeper understanding of it.

The Replace function accepts two arguments, the first argument is a string or regular expression, and the first argument can also accept a string, or possibly a function.

First, we no longer need to say "I am a Boy" for the first parameter as a string. Replace ("Boy", "Girl"), output: "I am a Girl". What I want to say here is the case where the first argument is regular. For regular expressions, the substitution behavior is first determined by the global (global//g), and if all substitutions are replaced, the Non-global only replaces the first matching string. For example:

"Ha ha". replace (/\b\w+\b/g, "he")  //him
 
"Ha ha". replace (/\b\w+\b/, "he")  //he ha

1: The second parameter is a string:

For a regular replace convention, a special marker $: $i (i:1-99): represents text that matches a left-to-right subexpression. $&: Represents full text that matches a regular expression. $ ' (': Toggle Skill Key): Indicates the left text of the matching string. $ ' (': single quote): The right text that represents the matching string. $$: Represents the $ transfer.

Here are a few demo:

"Boy & Girl". Replace (/(\w+) \s*&\s* (\w+)/g, "$ & $")//girl & Boy
 
"Boy". Replace (/\w+/g, "$&-$ & ")//Boy-boy
 
" JavaScript ". Replace (/script/," $&!= $ ")//javascript!= java
 
" JavaScript ". Replace (/ java/, "$&$ ' is"]//JavaScript is script

2: The second parameter is a function:

The ECMASCRIPT3 is recommended for use in the JavaScript1.2. The function is called every time the Replace method executes, returning the value as the new value for substitution.

Function arguments: The first argument is a full-text copy ($&) that matches each time. The middle argument is a subexpression that matches a string with an unlimited number. ($i (i:1-99)) The penultimate argument is the matching subscript position for the matching text string. The last parameter represents the string itself.

This is what this article says about replace power, the theory is dry goods, we need examples to solve all the empty problems:

1: Capitalize the first letter of the string:

String.prototype.capitalize = function () {return
 
    this.replace (/(^|\s) ([A-Z))/g, function (M,P1,P2) {return p1+ P2.touppercase ();
 
    } );
 
};
Console.log ("I am a boy!"). Capitalize ())//<span style= "Color:rgb (51, 51, 51); Font-family:georgia, "Times New Roman", Times, Sans-serif; font-size:14px; line-height:25.2000007629395px; " >i Am A Boy!</span>
2: The string "Zhang 356 points, Lee 474 points, Wang 592 points, Zhao 684 points" of the score extraction summary, calculate the average points and output everyone's average score gap.
var s = "Zhang 356 points, Lee 474 points, Wang 592 points, Zhao 684 points";
var a = S.match (/\d+/g);
var avg = a.reduce (function (obj, item) {return obj + parsefloat (item);},0)/a.length; 
var result = S.replace (/(\d+) Sub/g, function () {
 
            var n = parsefloat (arguments[1));
 
            return n + "minute" + "(+ (n > Avg)?" ("Out of average points" + (N-AVG)):
 
                        ("below average score" + (AVG-N))) + "cent";
 
});
Console.log (result);

Output:

Zhang 356 points (below the average of 20.5 points), Lee 474 points (below the average of 2.5 points), Wang 592 points (exceeding the average of 15.5 points), Zhao 684 points (exceeding the average of 7.5 points)

The Replace function of JavaScript plus the advanced application of the regular, JavaScript replace will send back a greater power, where it will no longer delve into the regular advanced application assertions.


Related Article

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.