Get the App
SLTechnology News&Howtos  ›  Development  › 

How to replace all specified strings by javascript

Shulou Source: shulou.com Published: 2022-06-03 17:38:23 10月15日 Update

This article mainly explains "javascript how to replace all the specified strings". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how javascript replaces all specified strings".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript replace () method

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

Syntax format:

String.replace (searchvalue,newvalue)

Searchvalue: yes. A RegExp object that specifies the substring or the schema to replace.

Note that if the value is a string, it is treated as an immediate amount of text pattern to retrieve, rather than being first converted to a RegExp object.

Newvalue: required. A string value. Specifies the function that replaces the text or generates the alternate text.

The second parameter of the replace () method can use the function, which is called when the match occurs, the return value of the function is used as the replacement text, and the function can receive special characters prefixed with $to reference information about the matching text.

The special character convention string in the second parameter of the replace () method describes $1, $2,..., $99 text matching the 1st through 99th subexpression in the regular expression $& (dollar sign + hyphen) substring $'(dollar sign + switch skill key) matching regular expression $'(dollar sign + single quotation mark) text $'(dollar sign + single quotation mark) to the left of the matching substring $$represents the $string to the right of the matching string

Return value: a new string, obtained after replacing the first match or all matches of regexp with replacement.

Example: the replace () method replaces all strings

Var str= "hello"; var a=str.replace ("hello", "HI"); console.log (a)

Thank you for your reading, the above is the content of "how javascript replaces all the specified strings". After the study of this article, I believe you have a deeper understanding of how javascript replaces all the specified strings, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Characters strings text functions methods expressions regular symbols learning special content parameters objects patterns size middle use information prefixes right at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology Redmi vpn NVidia