The Replace method in JavaScript

Source: Internet
Author: User

Definition and usage

The replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.

Stringobject.replace (regexp/substr,replacement)
Parameters Description
Regexp/substr

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

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

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

Regular characters

Replacevalue can be a string. If there are several specific characters in the string, it is converted to a specific string.

Character Replace text
$$ Direct volume symbol (just as ' $$ ' word trailing characters)
$& A string that matches a regular match
$` Match the character to the left of the string
$ Match the character to the right of the string
$1,$2,$,3,..., $n Matching results for the corresponding grouped matches

Regular Replacement string
' I am loser,you is loser '. Replace (/loser/g, ' hero ')//I am hero,you are hero

Use regular expressions and change the regular global property to True to make all loser become hero

Use $& characters to add parentheses to matching characters
var sstr= ' discusses the use of Replace in regular expressions '; Sstr.replace (/Regular expression/, ' {$&} '); // discuss the use of Replace in {regular expression}
Replace content with $ ' and $ ' characters
' ABC '. Replace (/b/, "$ '"); // AAC' abc '. Replace (/b/, "$ '"); // ACC
Combine new strings with grouping matches
' [email protected] '. Replace (/(. +) (@) (. *)/, "$2$1")//@nimojs
The Replacevalue parameter can be a function

The replacevalue in Stringobject.replace (Searchvalue,replacevalue) can be a function.
If Replacevalue is a function, then the arguments of this function will have n+3 parameters (the number of times that n matches the regular match)

First look at examples to help understand:

function logarguments () {        console.log (arguments); //  return     ' returns ' value will replace the match to the target '}console.log    (' [email protected] '. Replace (/(. +) (@) (. * )/, logarguments))

Parameters are

    1. Match to the string (this example is [email protected], it is recommended to modify the above code of the regular to see the matching characters to help understand)
    2. If the regular uses a grouping match for more than one otherwise there is no this parameter. (The parameters for this example are "Nimojs", "@", "126.com", respectively.) Recommended modification Regular is/nimo/view the arguments value returned in console)
    3. Match the corresponding index position of the string (this example is 0)
    4. Raw string (This example is [email protected])
Use a custom function to change the a-g string to lowercase
' JAVASCRIPT '. Replace (/[a-g]/g,function() {    return arguments[0].tolowercase ();}) // JaVaScRIPT
Use a custom function to make a callback replace the single quotation mark in the inline style
' <span style= ' font-family:\ ' Microsoft Ya Black '; >demo</span> '. Replace (/\ ' [^ ']+\ '/g,function() {          var sresult=arguments[0 ];    Console.log (sresult); // ' Microsoft Ya Black '    Sresult=sresult.replace (/\ '/g, ');    Console.log (sresult); // Microsoft Ya-Black    return Sresult;}) // <span style= "font-family: Microsoft Jacob Black;" >demo</span>

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.