In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to write javascript regular expressions". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
1. Create regular expressions
The first way: Note that the regular expression here cannot use single quotes or double quotes, as follows
var pattern1 = /[abc]/i; //matches the first "a" or "b" or "c", case insensitive
The second way: use RegExp constructor to create, the constructor passes two parameters, both are strings, so special attention needs to be paid to the conversion of "\" symbols, all metacharacters (there are metacharacters below) need double escape, as follows
The copy code is as follows:
var patt1 = new RegExp("[abc]","gi"); //here equivalent to var patt1 = /[abc]/gi;
alert("[abc]".match(patt1)); // return a
var patt2 = new RegExp("\\[abc\\]","gi"); //here equivalent to var patt2 = /\[abc\]/gi; while "\" in quotes must be transferred to "\\"
alert("[abc]".match(patt2)); // return [abc]
Here are two questions to consider:
a. If the regular expression here is a dynamic combination of a string and a variable, how to use the first way to create a regular expression?
Obviously, the second way to create a regular expression is not a problem, because the first argument passed in is itself a string. To create it in the first way, you need to use the eval() function, as follows
The copy code is as follows:
var str = "abc"; //This may be a dynamic variable
var patt1 = eval("/\\["+str+"\\]/"); //Equivalent to var patt1 = /\[abc\]/;
alert("[abc]".match(patt1)); // return [abc]
B. What is the difference between the above two creation methods?
Javascript advanced programming (third edition) says: the difference is whether to share, using the first way to create regular, shared, the second does not share. After the landlord practice found that the results are a little different, the two ways to create the same results, are (true true), you can test yourself. So MARK here, there's no difference between the two ways of creating.
The copy code is as follows:
var re = null,i;
for(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.