In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use replace new RegExp in JavaScript. The content is very detailed. Interested friends can refer to it for reference. I hope it can help you.
The syntax of the replace method is: stringObj.replace(rgExp, replaceText) where stringObj is a string (string), reExp can be a regular expression object (RegExp) or a string (string), and replaceText is the string found instead. To help you understand better, here is a simple example
var stringObj="Zhonggu People's Republic, Zhonggu People";//Replace typo "Zhonggu" with "China"//and return new characters after replacement//The value of original string stringObj has not changed var newstr=stringObj.replace("Zhonggu","China");alert(newstr);
Smarter than me, after reading the above example, you will find that the second typo "Zhonggu" has not been replaced with "China". We can perform the second replace method to replace the second typo "Zhonggu". After the program has been improved, it is as follows:
var stringObj="Zhonggu People's Republic, Zhonggu People";//Replace the typo "Zhonggu" with "China"//and return the new character after replacement//The value of the original string stringObj has not changed var newstr=stringObj.replace("Zhonggu","China");newstr=newstr.replace("Zhonggu","China");alert(newstr);
We can think carefully, if there are N N power typos, is it necessary to perform N N power replace method to replace the typos? Oh, don't be afraid, after having regular expressions, you don't need to execute a replace method without a typo. The improved code is as follows
var reg=new RegExp("Zhonggu","g"); //Create regular RegExp object var stringObj="Zhonggu People's Republic, Zhonggu People";var newstr=stringObj.replace(reg,"China");alert(newstr);
The above is the simplest application of the replace method. I don't know if you understand it. Let's start with a slightly more complex application.
When you search for articles on some websites, you will find such a phenomenon, that is, the search keywords will be highlighted and changed colors. How did this happen? In fact, we can use regular expressions to achieve, how to achieve it specifically? For a simple example, see the code below.
var str="People's Republic of China, People's Republic of China";var newstr=str.replace(/(person)/g,"$1");[xss_clean](newstr);
The above program lacks interactivity, we improve the program again, to achieve independent input to find the characters
var s=prompt("Please enter the character you are looking for","person");var reg=new RegExp("("+s+")","g");var str="People's Republic of China, People's Republic of China";var newstr=str.replace(reg,"$1");[xss_clean](newstr);
Maybe everyone doesn't understand what the special character $1 means. In fact,$1 means the character in parentheses in the left expression, that is, the first sub-match. Similarly,$2 means the second sub-match. What is a child match? In layman's terms, each parenthesis on the left is the first word match, and the second parenthesis is the second child match.
When we want to find the characters to carry out the operation, how to achieve it? Before we implement it, let's talk about how to get the parameters of a function. Inside Function, there is an arguments collection. This collection stores all the parameters of the current function. All the parameters of the function can be obtained through arguments. For everyone to understand, please see the following code.
function test(){alert("Number of arguments: "+arguments.length);alert("Value of each argument: "+arguments[0]);alert("Value of the second argument"+arguments[1]);//You can use for loop to read all arguments}test("aa","bb","cc");
After understanding the above program, let's look at the following interesting program
var reg=new RegExp("\d","g");var str="abd1afa4sdf";str.replace(reg,function(){alert(arguments.length);});
We were surprised to find that the anonymous function was executed twice, and there were three parameters in the function. Why was it executed twice? This is easy to think of, because we wrote a regular expression that matches a single number, and the string being detected happens to have two numbers, so the anonymous function is executed twice. What are the three parameters inside the anonymous function? To understand this, let us look at the code below.
function test(){for(var i=0;i
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.