All the strings in JS are replaced

Source: Internet
Author: User

No more nonsense, direct hair results.

In JS, the whole string substitution can be in the following ways:

 

Str.replace (/string/g to be replaced, "new string")

Like what:

"Yyyy-mm-dd-hh-mm-ss". Replace (/-/g, "/")

The results are as follows:
"Yyyy/mm/dd/hh/mm/ss"

Principle See JavaScript Replace () method introduction

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.

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

Description

The replace () method of the string Stringobject performs a find-and-replace operation. It looks for substrings in Stringobject that match regexp, and then replaces them with replacement . If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matched substring.

replacement can be a string, or it can be a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it shows that the resulting string from a pattern match will be used for substitution.

character Replace text
$, $ 、...、 The text that matches the 1th to 99th sub-expression in RegExp.
$& A substring that matches the regexp.
$` The text that is located to the left of the matching substring.
$ The text on the right side of the matching substring.
$$ Direct volume symbol.

Note: ECMAScript v3 stipulates that the parameter replacement of the replace () method can be a function instead of a string. In this case, each match calls the function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next parameter is a string that matches the subexpression in the pattern and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself.

JS string Replacement Reference: http://www.w3school.com.cn/jsref/jsref_replace.asp

JS Regular use reference: http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp

Original address: https://www.cnblogs.com/cblogs/p/9293522.html

Reprint Annotated Source

All the strings in JS are replaced

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.