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

How to use replace function in JavaScript

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is to share with you about how to use the replace function in JavaScript, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

The regular expression replace () function:

This function replaces the substring in the string that matches the regular expression with the specified string.

The return value is a new string after replacement.

Only the operations related to regular expressions are described here, and other replacement operations can be found in the article replace () method of the String object of javascript.

Grammatical structure:

StringObject.replace (regexp,replacement)

The list of parameters is as follows:

Parameter name semantic interpretation regexp is required. RegExp object. Replacement is required. A string value. Specifies the function that replaces the text or generates the alternate text.

Example code:

Var str= "I love jb51"; var reg=/j (?:\ w) + 1 Universe console.log (str.replace (reg, "girls"))

The above code replaces a substring in a string that can be matched by a regular expression with "girls".

Special note:

Replacement can be either a string or a function. If it is a string, each match will be replaced by a string.

The $character in replacement can have a specific meaning, as shown in the following table:

Character semantics explain the text of $1, $2,..., and $9 that matches the first to ninth subexpressions in regexp. $& the substring that matches regexp. $$direct measure symbol. The text before lastMatch in the $`input string. The text after lastMatch in the $'input string.

If replacement is a function, you can refer to the section on parameters when the second argument of javascript replace () is a function.

Example code:

Var str= "jb51,net"; var newStr=str.replace (/ (\ w +), (\ w +) /, "$2 $1"); console.log (newStr)

Output: net jb51

Function function: the replace function returns a copy of the string after text replacement according to the regular expression.

Function format: stringObj.replace (rgExp, replaceText)

Parameters: string stringObj,rgExp regular expression, what is replaced by replaceText

The contents of this article include string creation, regular expression implicit object creation, regular expression creation, and replace method matching.

Sample code:

/ / the contents of the string to be replaced var objStr=new String ("Designed By Androidyue"); / / implicitly create a regular expression object var reG=/e/w?/g;// global match (g is the global match parameter), match e or e? The content var re=/e/w?/;// does not specify a parameter, and the value matches once with (document) {write ("string before matching" + objStr+ "

"); write (" string with global matching "+ objStr.replace (reG," * ") +"

"); write (" match, do not specify parameter values for a match "+ objStr.replace (re," × ");} above is how to use the replace function in JavaScript. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more through this article. For more details, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report