Notes on regular expressions (3)



Careful readers may find that we missed the last articleString.replaceThis is the way.String.replaceIt has more powerful usage and flexibility in JS, so it’s separated here and introduced separately.


String.replace(searchValue, replacement)

String.replaceAt the same time, support regular expression or string replacement, and return a new string. Because our topic is regular expressions, the following content will focus on regular replacement.

By default,String.replaceReplace only once. If setgPattern, all matched strings are replaced

Parameter description

  • String: the string to be replaced
  • SearchValue: string or regular expression
  • Replacement: string or function


String replacement

'I am back end developer'.replace('back','front');
//"I am front end developer"

Directly put thebackreplace withfront. When there are twobackWhat’s going on?

'I am back end developer, you are back end developer'.replace('back','front');
//"I am front end developer, you are back end developer"

As you can see, the second oneback, has not been replaced. If you need to put the otherbackWe need to use regular expressions at this time.

Regular expression substitution

Set upgPattern, global replacement.

'I am back end developer, you are back end developer'.replace(/back/g,'front');
//"I am front end developer, you are front end developer"

stayreplacementString, there are some special variables can be used.

Special variables explain
$1,$2,$3…$n Represents the text that matches the corresponding capture group
$& String matching regular
$$ ‘$’ character
$` Matches the character to the left of the string
$’ Matches the character to the right of the string

Interesting string replacement

use$&The string that the operation matches.

Var STR ='interesting string replacement ';
str.replace (/ interesting string /, '($)');

//"(interesting string) substitution"


Var STR ='the price of this commodity is 12.99 ';

//"The price of this product is $12.99"

Replace content with $’and $’characters


Use group matching to combine new strings


Function parameters

WhenreplacementWhen it is a function parameter, the flexibility of string operation will be improved.


    console.log(arguments) // ["Stirng.replace", "Stirng", ".", "replace", 0, "Stirng.replace"]
    Return 'the return value will replace the matching character'
parameter explain
match The string (in this case String.replace )
p1, p2, … Regular uses grouping text when grouping matching, otherwise there is no such parameter (this example is “Stirng”, “.”, “replace”)
offset The corresponding index position of the matching string (0 in this example)
source The original string (in this case String.replace )

Case — Transformation of style attribute

Convert hump character to-Connector form

    return '-'+ m.toLowerCase()


hold-Convert connector form to hump form

    return s.toUpperCase()


The final test

The content given to the reader:

Need toHello-WorldReplace with regularHWeolrllod