Home  >  Article  >  Web Front-end  >  How to replace all characters in javascript string

How to replace all characters in javascript string

青灯夜游
青灯夜游Original
2021-06-17 15:32:544517browse

In JavaScript, you can use the replace() function to replace all characters in a string; this function is used to replace some characters with other characters in a string, or to replace a string that matches a regular expression. Substring, the syntax format is "str.replace(/original string/, "replacement text")".

How to replace all characters in javascript string

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript string replace all characters

<script type="text/javascript">

var str="Visit Microsoft!"
console.log(str.replace(/Visit Microsoft!/, "hello world"))

</script>

Output:

How to replace all characters in javascript string

Related function description:

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

Syntax

stringObject.replace(regexp/substr,replacement)
Parameters Description
regexp/substr

Required. A RegExp object that specifies the substring or pattern to be replaced.

Note that if the value is a string, it is retrieved as a literal text pattern, rather than first being converted to a RegExp object.

replacement Required. A string value. Specifies functions for replacing text or generating replacement text.

Return value

A new string obtained by replacing the first match or all matches of regexp with replacement.

Explanation

The replace() method of string stringObject performs a search and replace operation. It will look for substrings in stringObject that match regexp and replace those substrings with replacement. If the regexp has the global flag g, then the replace() method replaces all matching substrings. Otherwise, it only replaces the first matching substring.

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

Special characters in the second parameter of the replace() method
Conventional string Description
$1, $2, ..., $99 Text matching the 1st to 99th subexpressions in the regular expression
$& (Dollar sign hyphen) Substring matching the regular expression
$' (Dollar sign toggle skill key) is located Matches the text on the left side of the substring
$' (dollar sign single quote) Matches the text on the right side of the string
$$ represents $ string

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to replace all characters in javascript string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn