_javascript tips for replace use of String objects in JS

Source: Internet
Author: User
Today, in reading Qwrap source Stringh, there is a
Copy Code code as follows:

Format:function (S, arg0) {
var args = arguments;
Return S.replace (/\{(\d+) \}/ig, function (A, b) {
return args[(b | 0) + 1] | | '';
});
}

It is used in the following ways:
Alert (Format ("{0} love {1}.", ' I ', ' You '))//i
The format is implemented primarily by using the Replace method of a String object:

Replace: Returns a copy of the string that is replaced with text based on the regular expression.

1. Replace that is usually used
Copy Code code as follows:

function Replacedemo () {
var r, re; Declare a variable.
var ss = "The man hit the ball with the bat.\n";
SS = "While the fielder caught the ball with the glove."
re =/the/g; Creates a regular expression pattern.
r = Ss.replace (Re, "A"); Replace "the" with "A".
return (R); Returns the replaced string.
}
Replacedemo (); A Mans hit the ball with the bat. While the fielder caught is the ball with the glove.

2. subexpression in substitution mode
Copy Code code as follows:

function Replacedemo () {
var r, re; Declare a variable.
var ss = "The rain in Spain falls mainly in the plain.";
Re =/(\s+) (\s+) (\s+)/g; Creates a regular expression pattern.
r = Ss.replace (Re, "$3$2$1"); Exchange each pair of words.
return (R); Returns the result string.
}
document.write (Replacedemo ()); Rain the Spain in mainly falls the "in plain".

Matching regular items: The Rain,in spain,falls mainly,in the execution ss.replace (re, "$3$2$1") operation to complete the exchange of Word locations

The match is the first one (\s+)

The $ match is (\s+)

The $ $ match is the second one (\s+)

3.replace when the second parameter is a function

Copy Code code as follows:

function f2c (s) {
var test =/(\d+ (\.\d*)?) f\b/g; Initialization mode.
Return (S.replace test,function ($0,$1,$2) {return ((($1-32) + "C");});
}
F2C ("Water boils at 212F 3F. 2F 2.2F. 2");//water boils at 180c-29c. -30c-29.8c. 2

$ match 212f,3f,.2f,2.2f
Match 212,3,.2,2.2
$ match last. 2
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.