In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the replace method of using regular expressions to replace strings". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn what is the replace method of replacing strings with regular expressions.
Regular expression (regular expression) describes a string matching pattern (pattern), which can be used to check whether a string contains a certain substring, replace a matching substring, or extract a substring from a string that meets a certain condition.
Grammar
Str.replace (regexp | substr, newSubStr | function)
Parameters.
Regexp (pattern)
A RegExp object or its literal quantity. The content matched by the rule is replaced by the return value of the second parameter.
Substr (pattern)
A {{jsxref ("String", "string")}} to be replaced by newSubStr. It is treated as an entire string rather than a regular expression. Only the first match will be replaced.
NewSubStr (replacement)
{{jsxref ("String", "string")} used to replace the matching part of the first parameter in the original string
Function (replacement)
A function used to create a new substring whose return value replaces the result matched by the first parameter.
Return value
One or all matches a new string that is replaced by an alternative pattern.
Description
The above syntax means that there are two parameters, but the two parameters can transfer values of different types.
Maybe you haven't understood the BB above for a long time, it doesn't matter, let's go straight to the code:
Usage
1. Basic usage
Var x = "abcda"; / / both parameters are the string var y = x.replace ('axiomain'); / / 'xbcda'
1. Advanced usage
Var x = "abcda"; / / the preceding parameters are regular, followed by the string var y = x.replace (/ a _ xbcdx' g _ ref _ x'); / / 'regular.
1. Complex usage
Var x = 'abcda';// is preceded by the regular parameter followed by the string var y = x.replace (/ a _ match) {return match.toUpperCase ();}); / /' AbcdA'
Application scenario
Here we only talk about the actual use of complex usage scenarios, for example, there is a requirement: you need to replace the character an in all tags in the html file with the character b. Of course, if it is the regular god, you can skip here. The following ways are easier to understand and more suitable for regular novices.
Solution:
Var allData = "" +'\ r\ nfolk + "aaa" +'\ r\ nfolk + "
Aaa
"+'\ r\ ncustomers +"; allData = allData.replace (/\ ([\ s\ S] *?)\ / g, function (match) {return match.replace (/\ (. *?)\ / g, function (match2) {return match2.replace (/ a _ Unig, "b")}))
PS: regular expressions need to include the g flag when doing global search replacements.
Thank you for your reading, the above is the content of "what is the replace method of replacing strings with regular expressions". After the study of this article, I believe you have a deeper understanding of what the replace method of replacing strings with regular expressions is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.