Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of replace in javascript

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the use of replace in javascript, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The javascript replace method, which is used to replace some characters in a string with some characters or a substring that matches a regular expression, uses the syntax "stringObject.replace (regexp/substr,replacement)".

This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer.

Detailed explanation of the usage of javascript replace

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.

Grammar

StringObject.replace (regexp/substr,replacement)

The parameter regexp/substr must be 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.

Replacement required: a string value. Specifies the function that replaces the text or generates the alternate text.

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

Description: the replace () method of the string stringObject performs the find and replace operation. It looks for substrings that match regexp in stringObject and replaces them with replacement. If regexp has the global flag g, then the replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.

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

Note: ECMAScript v3 states that the parameter replacement of the replace () method can be a function rather than a string. In this case, the function is called for each match, and the string it returns is used as replacement text. The first argument to this function is the string that matches the pattern. The next parameter is a string that matches the subexpression in the pattern, and there can be 0 or more such parameters. The next parameter is an integer that declares where the match occurs in the stringObject. The last parameter is stringObject itself.

Use the example

In this example, we will replace "Microsoft": var str= "Visit Microsoft!" [xss_clean] (str.replace (/ Microsoft/, "W3School") in the string with "W3School"

Output:

Visit W3School! Thank you for reading this article carefully. I hope the article "what is the use of replace in javascript" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report