Home > Web Front-end > JS Tutorial > body text

How to replace all characters in javascript string

青灯夜游
Release: 2023-01-06 11:17:39
Original
4515 people have browsed it

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>
Copy after login

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)
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template