JS Replace and ReplaceAll instance usage

Source: Internet
Author: User
Tags regular expression

JS Replace and ReplaceAll instance usage

Stringobj.replace (Rgexp, ReplaceText)

Parameters

Stringobj

Required option. The string object or string literal to perform the substitution. The string is not modified by the Replace method.

Rgexp

Required option. Is the regular expression object that contains the regular expression pattern or the available flags. It can also be a String object or text. If Rgexp is not a regular expression object, it is converted to a string and the exact lookup is made; do not attempt to convert the string to a regular expression.

ReplaceText

Required option. is a string object or string literal, and the position in each matching rgexp in Stringobj is replaced with the text contained in that object. In the Jscript 5.5 or later version, the ReplaceText parameter can also be a function that returns the replacement text.

Description

The result of the Replace method is a copy of the Stringobj object that completes the specified replacement.

Any of the following matching variables can be used to identify the latest match and to find a matching string. You can use a matching variable in a text replacement that requires a dynamic decision to replace a string.

Character meaning

$$ $ (JScript 5.5 or later)

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

$ ' Specifies the stringobj part before the match described by $&. (JScript 5.5 or later)

$ ' Specifies the stringobj part after the match described by $&. (JScript 5.5 or later)

$n the nth substring of the capture, where n is a decimal number from 1 to 9. (JScript 5.5 or later)

$nn the captured nn substring, where nn is the decimal two digits from 01 to 99. (JScript 5.5 or later)

If ReplaceText is a function, for each matching substring, the function is invoked with the following m+3 argument, where M is the number of left parentheses captured in Rgexp. The first argument is a matching substring. The next m parameter is all the results captured in the lookup. The m+2 parameter is the offset that appears in the Stringobj, and the m+3 argument is stringobj. The result is a string value that replaces each matched substring with the corresponding return value of the function call.

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

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 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.

}

Alternatively, the Replace method can replace the subexpression in the 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; Creates a regular expression pattern.

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."));

JS does not provide the ReplaceAll method, with a for loop and efficiency problems, give you a regular expression of the solution

JS Code

String.prototype.replaceAll = function (S1,S2) {

Return This.replace (New RegExp (S1, "GM"), S2);

}

Methods: String.Replace (New RegExp (oldstring, "GM"), newstring)

GM G=global, M=multiline, is generally the way to do this, you can implement the replacement of all the specified strings

Another simple way to verify JS:

Enter in the browser address bar

Javascript:alert ("Abcabcabc". Replace (new RegExp ("A", "GM"), "ad")

This is more convenient;), I do not know whether more than the line will be very handy

Orgstr.replace (New RegExp (FindStr, ' G '), REPLACESTR)

We should be able to replace all of them.

If you do not use regular expressions

Orgstr.replace (FINDSTR, REPLACESTR) can only replace the first

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.