This is the third day of my participation in the August Text Challenge.More challenges in August

preface

Yesterday, I wrote an article about using regular to achieve thousandth bit separation (the link is here), and then I used replace method. I thought I knew replace, but I found that I only knew the surface of it. So, I took this opportunity to learn replace method again.

grammar

Replace, as the name suggests, is a string replacement method. This substitution does not affect the original string.

str.replace(searchStr, newStr)

  • searchStr

    The string to be matched can be a string or a regular expression.

  • newStr

    The string to be matched can be a string or a function, which is replaced in the function.

    • If newStr is a string, inserting special variable names into the string is supported. The following variable names are supported:

      The variable name describe
      $$ Insert $
      $& Insert the matching substring
      $` Insert the content to the left of the match
      $’ Insert the content to the right of the match
      $n If searchStr is a regular expression, n represents the parentheses in the regular expression. N starts with 1, and 1 represents the content matched by the first parentheses in the regular expression, and so on. N cannot exceed 100. If the regular expression does not have the NTH parenthesis, $n is treated as a normal string.

      Here are some examples:

       'the answer cp3'.replace('cp3'.'$$')   // "answer $" inserts $instead of cp3
      
       'the answer cp3'.replace('cp3'.'$&')   // "answer cp3" inserts a matching substring, so the result is unchanged
      
       'the answer cp3'.replace('cp3'.'$`)  // "answer answer" inserts the content to the left of the match
      
       'the answer cp3'.replace(The 'with answers'."$")  // insert "cp3cp3" to the right and left of the match
      
       'the answer cp3'.replace(/(cp3)/."$1")  // "answer cp3", since $1 represents the first parenthesis of the regular expression and matches' cp3 ', $1 corresponds to 'cp3'
      
       'the answer cp3'.replace(/cp3/."$1")  // "answer $1", since there are no parentheses in the re, treat $1 as a normal string
      
      Copy the code
    • If newStr is a function, the function is executed after a match. If searchStr is a regular expression and a global match, the function is executed multiple times until the match ends.

      The function takes the following arguments (in order) :

      • match

        The matched substring

      • At $1, $2,… $n

        Corresponding to $n above, if searchStr is a regular expression, n represents the NTH parenthesis of the regular expression, starting at 1 and representing what the NTH parenthesis of the regular expression matches

      • index

        The first character of the matched substring (that is, the first argument match) is the subscript of the original string

      • str

        The original character entered

        Let’s take the example above

        // The re in this example has a parenthesis that corresponds to $1
        'the answer cp3'.replace(/(cp3)/.(match, $1, index, str) = > {
           console.log(match, $1, index, str) // cp3 cp3 2 answer cp3
           return 'cp3'
        }) // return the answer cp3
        
        // The re in this example has 2 parentheses, corresponding to $1,$2
        'the answer cp3'.replace((cp3) / / (the answer).(match, $1, $2, index, str) = > {
           console.log(match, $1, $2, index, str) // Answer cp3 answer cp3 0 answer cp3
           return 'the answer cp3'
        }) // return the answer cp3
        
        // In this example, the re uses global matching, and the function is executed multiple times
        'Answer cp3 answer cp3'.replace(/ (cp3)/g (the answer).(match, $1, $2, index, str) = > {
           console.log(match, $1, $2, index, str) 
           // First print: answer cp3 Answer cp3 0 answer cp3 Answer cp3 answer cp3
           // Second print: answer cp3 Answer cp3 5 answer cp3 Answer cp3 Answer cp3
           return 'the answer cp3'
        }) // return the answer cp3
        Copy the code

conclusion

The above is the use of replace summarized by myself. I have also learned it myself. I hope it will be helpful to you.