The detailed use method of JS character substitution function

Source: Internet
Author: User

Definitions and usage
The replace () method replaces some characters in a string with some other characters, or replaces a substring that matches a regular expression.
Grammar
Stringobject.replace (regexp/substr,replacement)
Parameter description
Regexp/substr
Necessary. The RegExp object that prescribes the substring or pattern to be replaced.
Note that if the value is a string, it is used as the direct text pattern to retrieve, not first converted to the RegExp object.
Replacement required. A string value. Provides a function that replaces text or generates alternate text.
return value
A new string that is replaced with replacement for the first match of RegExp or after all matches have been made.

Example
The following example shows how the Replace method replaces the first occurrence of the word "the" with the use of the word "A".

   
   function Replacedemo () {
    var r, re;//declare 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;//create regular expression patterns.
    r = ss.replace (Re, "a");//Replace "the" with "a".
    return (R);//returns the replacement string.
  }
   Alternatively, the Replace method can also replace a subexpression in a pattern. The following example shows each pair of words in a swap string:
   
   function Replacedemo () {
    var R, re; Declare a variable.
    var ss = "The rain in Spain falls mainly in the plain.";
    re =/(s+) (s+) (s+)/g;//create regular expression patterns.
    r = ss.replace (Re, "$3$2$1");//exchange each pair of words.
    return

(r); Returns the result string.
}
The following example (performed in JScript 5.5 and later) performs a conversion from Fahrenheit to Celsius, demonstrating the use of functions as replacetext. To see how the function works, pass a string containing numeric values followed by "F" (for example, "Water boils at 212").

function f2c (s) {
var test =/(d+ (. d*)?) fb/g; Initialization mode.
Return (S.replace
(Test,
function ($0,$1,$2) {
Return (($1-32) * 5/9) + "C");
}
)
);
}
document.write (f2c ("Water freezes at 32F and boils at 212F."));

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*)?) fb/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

Description
The replace () method of the string Stringobject performs a find-and-replace operation. It will look for substrings in the stringobject that match the regexp, and then replace them with replacement. If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.
Replacement can be either a string or a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it shows that the string that is matched from the pattern will be used to replace the

Replace is not sensitive to case

<body>

<script type= "text/web Effects" >

Text = "JavaScript Tutorial";
document.write (Text.replace (/javascript/i, "JavaScript"));

</script>

</body>

JavaScript Tutorial

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.