Js replace and replaceall instance usage

Source: Internet
Author: User

Js replace and replaceall instance usage

StringObj. replace (rgExp, replaceText)

Parameters

StringObj

Required. String object or String text to be replaced. The string is not modified by the replace method.

RgExp

Required. It is a regular expression object that contains the regular expression mode or available flag. It can also be a String object or text. If rgExp is not a regular expression object, it is converted to a string for exact search. Do not convert the string to a regular expression.

ReplaceText

Required. Is a String object or String text. For each position in stringObj that matches rgExp, it is replaced by the text contained in this object. In Jscript 5.5 or later versions, the replaceText parameter can also be a function that returns the replaced text.

Description

The result of the replace method is a copy of the specified replaced stringObj object.

Any of the following matching variables can be used to identify the latest matching and find the matching string. Matching variables can be used in text replacement that dynamically determines the replacement string.

Character meaning

$ (JScript 5.5 or later)

$ & Specifies the part of stringObj that matches the entire pattern. (JScript 5.5 or later)

$ 'Specifies the stringObj section before matching by $. (JScript 5.5 or later)

$ 'Specifies the stringObj part after matching by $. (JScript 5.5 or later)

$ N indicates the nth child matching captured. Here, n is the first digit in decimal format from 1 to 9. (JScript 5.5 or later)

$ Nn: the nn sub-match captured by nn. nn is a two-digit decimal number from 01 to 99. (JScript 5.5 or later)

If replaceText is a function, for each matched sub-string, the function is called with the following m + 3 parameters. Here m is the number of left arc captured in rgExp. The first parameter is a matched substring. The following m parameters are all the results captured in the search. The m + 2 parameter is the offset that matches in stringObj, and the m + 3 parameter is stringObj. The result is to replace each matched substring with the corresponding return value of the function call.

The Replace method updates the attributes of the global RegExp object.

Example

The following example demonstrates how to replace The first occurrence of The word "The" with The word "A" in The replace method.

Function ReplaceDemo (){

Var r, re; // declare the 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 The regular expression mode.

R = ss. replace (re, "A"); // replace "The" with "".

Return (r); // return the replaced string.

}

In addition, the replace method can replace the subexpression in the mode. The following example demonstrates each pair of words in the exchange string:

Function ReplaceDemo (){

Var r, re; // declare the variable.

Var ss = "The rain in Spain falls mainly in the plain .";

Re =/(S +) (s +) (S +)/g; // create the regular expression mode.

R = ss. replace (re, "$3 $2 $1"); // exchange each pair of words.

Return (r); // return the result string.

}

The following example (executed in JScript 5.5 and later versions) is a conversion from Fahrenheit to Celsius, which demonstrates the use of functions as replaceText. To know how the function works, pass a string containing a value 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 ."));

Js does not provide the replaceAll method. It is efficient to use the for loop. It provides you with a regular expression solution.

Js Code

String. prototype. replaceAll = function (s1, s2 ){

Return this. replace (new RegExp (s1, "gm"), s2 );

}

Method: string. replace (new RegExp (oldString, "gm"), newString ))

Gm g = global, m = multiLine. In general, this method can replace all specified strings.

Another simple method to verify JS:

In the browser address bar, enter

Javascript: alert ("abcabcabc". replace (new RegExp ("a", "gm"), "ad "))

This is easy to use.) I don't know if multiple rows will be very convenient.

OrgStr. replace (new RegExp (findStr, 'G'), replaceStr)

It should be enough to replace all of them.

If regular expressions are not required

OrgStr. replace (findStr, replaceStr) can only replace the first

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.