String.Replace and String.Format

Source: Internet
Author: User

Replace function for string replace the usual frequency is very high, and the Format function is commonly used to fill in placeholders. Here is a brief summary of the use of these two functions.

First, two usages of string.replace

Replace is used such as replace (RegExp, STRING|FN); The first argument is a regular expression, the second argument can be a string to replace, or a function with a return value, its function is to replace the second argument with a matching value.

1.replace (RegExp, string): I want to replace "small" in "little" with "big", as shown below.

Console.log ("Le Little Days". Replace (/small/g, "big")); // Le Tai Day

// the way trim is implemented console.log (" le Xiao Tian ". Replace (/(^\s+) | ( \s+$)/g, "")); // le little Day

2.replace (RegExp, FN); FN This callback function can have four parameters, the first parameter is a string that matches regexp, the second is a string that matches the RegExp subexpression (there are several self-expressions, and several arguments are deferred, if there are no sub-expressions, The second parameter is the third parameter), the third parameter is regexp matches the string's index in the string, and the fourth parameter is the string that the replace is currently called.

Take the implementation of trim above as an example, its regular expression contains two sub-expressions: (^\s+) and (\s+$), so the callback function should have 5 parameters. Of course, if there are no sub-expressions, there are only three types of parameters.

Console.log ("Le Little Days". Replace (/(^\s+) | ( \s+$)/G,function(Match, MatchChild1, MatChild2, Index, strobj) {Console.log ("Match:" + match + ";"); Console.log ("MatchChild1:" + matchChild1 + ";"); Console.log ("MatChild2:" + matChild2 + ";"); Console.log ("Index:" + index + ";"); Console.log ("Strobj:" +strobj + ";"); return""; }));/**match: matchChild1:; matchild2:undefined;index:0;strobj: Lechao; match:; Matchchild1:undefin Ed;matchild2:; Index:11;strobj: Lechao; le Xiao Tian*/
Second, the realization of String.Format

Sometimes we don't know in advance what the string counterpart should be, so we use the placeholder "{number}" to make a pre-placeholder in the string and replace it when it's really OK. To put it bluntly, the unknown substitution character is encapsulated as a parameter, and the invariant part of the substitution logic is separated from the change part of the replacement parameter.

1.format implementations:

// Extended Format function () {    var args = arguments;     return  This function (match, number) {        returntypeof Args[number]! = ' undefined '? Args[number]: Match;    }); Console.log ("{0} is a {1} handsome!"). Format (' Monkey King ', ' big ')); // Monkey King is a handsome brother!

2.format replacement characters are used more in form validation, such as the miniui of the front-end UI Framework VType is defined:

Mini. Vtypes = {   "date can not is less than {0}",   "Date can is not is greater than {0}",   ...  };

Replace the corresponding boundary value with "{0}" when returning an error message

  

  

String.Replace and String.Format

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.