In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to achieve regular expression highlight highlight, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Programmers have to deal with strings and "queries" when writing code. In the intersection of the two, there is something called regular expressions, which can improve programming efficiency when used well. You can learn it hard first.
With regard to the use of regularities, take a simple example:
Var m = location.href.match (/ (\ w):)\ / {0jue 3} ([^\ /] +) (?: (\ / [^\? #] *))? (?: (\? [^ #] +))? (?: (#. *))? /)
Var res = {
Protocol: m [1]
Host: m [2]
Path: m [3]
Search: m [4]
Hash: m [5]
}
Console.log (res)
Held back for a few minutes = = |, I also think this rule is not very easy to understand (red and black, should be a little clearer), some friends may not know what (?: content) means, there are similar (?! content) and (? = content) grouping and forward-looking knowledge, it is recommended to ask du GE and Gu Niang.
one。 Regular Application Mini DEMO example
Here is a string of handwritten code that is placed in textarea:
/ * *
* @ author barret lee
* @ date 2013-10-06
* @ email barret.china@gmail.com
, /
/ / outer var
Var a = "this id outer string"
/ / closure
Function b () {
/ / inner var
Var a = "this is inner string"
Var g = a.replace (/ this is inner string/g, function () {
Return new Function ("/ * clousure*/this.a") ()
});
/ * *
* @ description closure-regExp test
* @ author barret lee
, /
Function c () {
Return {
A: a
G: g
}
}
Return c
}
Var s = b () (); / / s.a, s.g
Regular match, dealing with the small DEMO of the above pile of strings:
The copy code is as follows:
/ * *
* @ author barret lee
* @ date 2013-10-06
* @ email barret.china@gmail.com
, /
/ / outer var
Var a = "this id outer string"
/ / closure
Function b () {
/ / inner var
Var a = "this is inner string"
Var g = a.replace (/ this is inner string/g, function () {
Return new Function ("/ * clousure*/this.a") ()
});
/ * *
* @ description closure-regExp test
* @ author barret lee
, /
Function c () {
Return {
A: a
G: g
}
}
Return c
}
Var s = b () (); / / s.a, s.g
Follow the hint, the next step is a few more times, you can see that the effect can still be dropped. The main reason is that the regularization here is slightly troublesome, and it takes a long time to deal with a color.
P.S: the above thing is just a small test, the code is not very robust, practice with learning rules.
two。 What you need to pay attention to.
The copy code is as follows:
/ / step config
Var config = {
Regs: [
/ ^\ s+ |\ sroomdrum g
/ (["']) (?:\\. | [^\\ n]) *?\ 1hand g
/ / (?!\ * | span). +\ / (?! span) [gim] * / g
/ (\ /\ / .* |\ / * [\ S\ s] +?\ * /) / g
/ (\ *\ s*) (@\ w+) (? =\ s*) / g
/\ b.\ bUnip g
]
/ /...
}
I wrote these rules separately, mainly to do the above demo to facilitate single-step display. But it is quite unreasonable to write these regularities separately. What is put in the textarea is a string without any tags. In order to color, some tags will be inserted into the codes for coloring. (I use span here). When we deal with the next regular, we have to consider the tags added in the previous step. This will greatly improve the difficulty of development. The best way is to put these rules into a lump. Then use | to separate:
The copy code is as follows:
Var reg = (/ ^\ s + |\ skeeper /) | (/ (["']) (?:\. | [^\ n]) *?\ 1 /) | (/\ / (?!\ * | span). + / (?! span) [gim] * /) | (/ (\ /\ / .* |\ / * [\ S\ s] +?\ *\ /) /) | (/ (\ *\ s*) (@\ w+) (? =\ s*) ) /) | (/\ b (break | continue | do | for | in | function | if | else | return | switch | throw | try | catch | finally | var | while | with | case | new | instance | delete | void | Object | Array | String | Number | Boolean | Function | RegExp | Date | Math | window | navigator | location | true | true | false | false)\ b)
The advantage of this is that it can be put directly.
Codes.replace (reg, function () {
Var args = arguments
/ /...
});
One-time processing, do not have to worry about inserting the tag will affect the final result, because the tag is inserted at the same time, there will be no interference.
The basic principle of the code highlighting plug-in highlight is similar, except that its fault tolerance mechanism and code robustness are more perfect. I added a configuration file to the code.
Var colors = {
String: "# FFA0A0"
Reg: "# 16E916"
Note: "# 888"
Tag: "orange"
Keywords: "# B0B0FF"
}
You can also modify it according to your own preferences, and for extensions like this, we should just pay a little attention to leaving a few more interfaces when writing the code. (but it's hard to do all this work.)
There is no plug-in, and it is not necessary to know the basic principles and then put it into practice.
The above is all the content of the article "how to achieve regular expression highlight highlighting". 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.