In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you how to use the regular expression replace method of string in js, which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn how to use the regular expression replace method of string in js.
The replace method is one of the more complex methods that javascript involves in regular expressions, which strictly speaking should be the method of string object. It's just that there are more regularities involved. We need to use it flexibly.
Syntax: stringObj.replace (regexp/substr,replacement)
First parameter: required. The substring or regular RexExp to be replaced in the string
The second parameter: required, a string value that specifies the function that replaces the text or generates the alternate text.
Return value: note that the return value is a new string and does not change the original string, which is obtained after replacing the first match or all matches of regexp with replacement.
Therefore, it can be divided into many situations according to its different parameters, and the following are analyzed one by one:
Both parameters of NO.1 are strings
Var str1 = 'this is the original text, the content that needs to be replaced' this needs to be replaced'! ; var newStr = str1.replace ('this is to be replaced', 'need replace'); console.log (newStr); / / output: this is the original text and the content to be replaced is "need replace"!
The second parameter string 'need replace',' in the above example replaces the first parameter string 'this is to be replaced'. This is the simplest form.
The first parameter of NO.2 is regular, and the second parameter is a string
Var str2 = 'this is the original text and the content that needs to be replaced is' ac to replace bb'! ; var newStr = str2.replace (/ ([a murz])) + / g console.log (newStr); / / output: this is the original text, the content that needs to be replaced is "qqq to replace qqq"!
The above example string 'qqq', replaces the content of the regular match. If regexp has the global flag g, then the replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.
The first parameter of NO.3 is regular, and the second parameter is a string with the character $.
Var str3 = 'this is an original text, "3c to replace 4d!'; var newStr = str3.replace (/ ([0-9]) ([Amurz]) / g," $1 "); console.log (newStr); / / output: this is an original text," 3 this is to replace 4 "!
In the above example, $1 means that the first sub-representation in regexp ([0-9]) matches a single number, and if it is $2, it means that the second sub-representation is ([a murz]); therefore, the whole of'3c'is replaced by'3' represented by the first sub-representation, and '4d' is replaced by the number' 4' that matches by the first sub-representation. Several others are available in the same way:
(/ ([0-9]) ([amurz]) / g, "$2")-> / output: this is the original text, "c, this is to replace d"!; (3c and 4d are replaced by the corresponding second sub-representation matching c and d) (/ ([0-9]) ([amurz]) / g, "$'")-> / output: this is the original text, "this is to replace d"! This is to replace "!"; (3c is replaced by 3c right text, 4d right is "! Replace, so it appears twice)
The first parameter of NO.4 is regular, and the second parameter function
Var str4 = 'this is the original text and the content that needs to be replaced is' aa, which requires bbb to replace ccccc'! ; var newStr = str4.replace (/ [amerz]] + / gMagneFunction ($0) {var str ='; for (var I = 0; I < $0.roomth; iTunes +) {str + ='*';}; return str;}); console.log (newStr); / / this is the original text and the content that needs to be replaced is "* * this needs to be replaced *"!
The first argument of the above example function is the whole of the matching regexp, and the value returned according to the length function is the corresponding replacement text.
The first parameter of NO.5 is regular and has a subexpression, and the second parameter function has multiple parameters.
Var str5 = 'this is the original text, the content that needs to be replaced is "3c, this is to replace 4d"! ; var newStr = str5.replace (/ ([0-9]) ([a Murz])) / g recorder function (arg1,arg2,arg3,arg4,arg5) {console.log (arg1); console.log (arg2); console.log (arg3); console.log (arg4); console.log (arg5);})
Output:
3c
three
C
seventeen
This is an original text, the content that needs to be replaced is "3c this will replace 4d"!
4d
four
D
twenty-three
This is an original text, the content that needs to be replaced is "3c this will replace 4d"!
In the above example, the first parameter arg1 represents the matching whole, arg2 represents the first subexpression, arg3 represents the second subexpression, and the next parameter arg4 is an integer that declares where the submatch appears in stringObject. The last parameter is stringObject itself.
The above is all the content of the article "how to use the regular expression replace method of string in js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.