The second parameter resolution of the replace () method in JavaScript

Source: Internet
Author: User

Grammar

string. Replace (searchvalue,newvalue)

parameter Values

Searchvalue
Have to. A RegExp object that specifies the substring or pattern to replace. Note that if the value is a string, it is used as the direct volume text pattern to be retrieved, instead of being converted to the RegExp object first.

NewValue
Necessary. A string value. A function that specifies replacement text or generates alternate text.

return value

String
A new string that is obtained after the first match or all matches of regexp have been replaced with replacement.

The first parameter of Searchvalue

This parameter is better understood, either as a string or as a regular expression. As noted above "note that if the value is a string, it is used as the direct volume text pattern to be retrieved, rather than being first converted to the RegExp object." , meaning that ‘/abc/gi‘ it will not be parsed as a regular object, even if it is written in the same way as regular.

This article does not say the first argument, only the second argument.

The second parameter newvalue

This parameter is either a normal string or a regular substitution expression string, which is either a string or a function.

Regular substitution expressions

Special substitution characters:

Character substitution text
$& a string that matches a regular match
$ ' matches the character to the left of the string
$ ' matches the character to the right of the string
$1,$2,$,3,..., matching results in $n match results

$&

$& for cases where there are no sub-expressions

var sstr= ' discusses the use of Replace in regular expressions '; Sstr.replace (/Regular expression/, ' $& "); // get: "Discuss the use of Replace in regular expressions"
$`

Match all characters to the left of the string

var sstr= ' discusses the use of Replace in regular expressions '; Sstr.replace (/Regular expression/, ' $ '); // get: "Discuss the use of Replace in the discussion"
$

Match all the characters to the right of the string, note that since $ ' has single quotation marks, then the outer quotation marks must be double quotes, and if you cannot double quotes, you can only escape the single quote of $ '.

var sstr= ' discusses the use of Replace in regular expressions '; Sstr.replace (/Regular Expression/, "$ '"); // get: "Discuss the use of Replace in the use of Replace in"
$, $, $, ..., $n

Match sub-expressions in turn

var sstr= ' discusses the use of Replace in regular expressions '; Sstr.replace(/(Regular) (. +?) (Formula)/, "$" $2<$3> "); // get: "Discuss the use of Replace in the" regular "Expression < >"
function

First look at arguments the usage:

var sstr= ' discusses the use of Replace in regular expressions '; Sstr.replace (/(Regular). +? ( )/,function() {    console.log (arguments);}); // ["Regular expression", "regular", "type", 4, "Discuss the use of Replace in regular expressions"]

The parameters were:

    • The string to match (this example is "regular expression")

    • If the regular uses a grouping match for more than one otherwise there is no this parameter. (The parameters for this example are "regular", "type")

    • Match the corresponding index position of the string (that is, the index position of the "regular expression", this example is 4)

    • Raw string

If the global identifier G is added, it is:

var sstr= ' discusses the use of the regular expression ' of Replace in regular expressions '; Sstr.replace (/(Regular). +? ( /g,function() {    console.log (arguments);}); // ["Regular expression", "regular", "type", 4, "discussion of the regular expression usage of Replace in regular expressions"] // ["Regular expression", "regular", "type", 19, "discussion of the regular expression usage of Replace in regular expressions"]

In other words, arguments is the built-in property of the current function, which refers to the current matching parameter pseudo-array. Arguments[0] is the currently matched string.

var sstr= ' discusses the use of the regular expression ' of Replace in regular expressions '; Sstr.replace (/(Regular). +? ( /g,function() {    console.log (arguments[0]);     return arguments[0] + ' a ';}); // Regular Expressions // Regular Expressions // "discusses the regular expression of replace in regular expression a usage"

The advantage of a function's argument is that it can do more complex operations on arguments than just a string parameter. Of course, arguments does not have to be used.

Original link: https://www.jianshu.com/p/31bebd90fd1d

The second parameter resolution of the replace () method in JavaScript

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.