I recently undertook a personal challenge of going through the jQuery source as a way of improving my understanding of Javascript and Javascript frameworks.
One of the first things I encountered when I took a look through the code was some daunting looking regular expressions. Now, I’ve had a good bit of exposure to regular expressions and know how powerful they can be, but it still took me a while to wrap my head around exactly what was going on.
Checking if a selector is HTML or an ID
// A simple way to check for HTML strings or ID strings // (both of which we optimize for) quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w-]+)$/,
This intimidating regular expression checks to see if the jQuery selector passed into the jQuery method is either an HTML string or an ID string. Here’s a breakdown of the regular expression (everything between the slashes):
- ^ - The beginning of the line
- [^<]* – 0 or more instances of anything but and open angle bracket (<)
- (<[\w\W]+>) – An open bracket, followed by one or more of any character, followed by a close bracket (an html string). Note that the parenthesis indicate that this portion of the regular expression should be matched.
- [^>]*- 0 or more instances of anything but and close angle bracket (<)
- $ – end of line
- | – OR
- ^ - The beginning of the line
- # – Hash character
- ([\w-]+) – One or more instances of word characters (a-z, A-Z, 0-9) or the minus sign (matched).
- $ – end of line
As described in step 6 above, this particular regular expression can be matched in two ways, if the first half is matched, then the expression contains a html string. If the second half is matched, then we have an id selector (like #someid). As the jQuery comment states, jQuery can optimize it’s code for those two conditions, and this regular expression lets it know if it has either of them.
Checking For a Simple Selector
// Is it a simple selector isSimple = /^.[^:#\[\.,]*$/,
This regular expression checks to see if it jQuery has been passed a “simple selector,” a selector that doesn’t have advanced qualifiers ( like [attr='value'], or :eq(0) ).
- ^ - The beginning of the line
- . - Any character
- [^:#\[\.,]* – 0 or more characters excluding colon, hash, open square brace, period, or comma
- $ – end of line
So in short, this expression matches any string that doesn’t contain the characters in step 3.
It can take some thought to figure out what a regular expression is saying if you aren’t using them all the time (at least for me). These are just a couple of the many regular expressions in the jQuery source. I’ll post other tidbits from the library as I make my way through it.
Trackbacks/Pingbacks
buy@generic.LEVITRA” rel=”nofollow”>……
Need cheap generic LEVITRA?…
Buy@Discount.Coral.Calcium” rel=”nofollow”>.…
Buyit now…
Buy@Abana.Online” rel=”nofollow”>.…
Buynow it…
Buy@Cheap.Abana” rel=”nofollow”>..…
Buygeneric pills…
Purchase@Abilify.Online” rel=”nofollow”>……
Buywithout prescription…
Buy@Generic.Abilify.5mg.10mg.15mg.20mg.30mg” rel=”nofollow”>……
Buywithout prescription…
Purchase@Discount.Acai” rel=”nofollow”>..…
Buywithout prescription…
Buy@Acai.Without.Prescription” rel=”nofollow”>……
Buynow…
Order@Cheap.Coral.Calcium” rel=”nofollow”>..…
Buyit now…
Buy@Cheap.Coral.Calcium” rel=”nofollow”>……
Buygeneric meds…
Cheap@Coral.Calcium.Online” rel=”nofollow”>..…
Buygeneric drugs…
Order@Discount.Abana” rel=”nofollow”>..…
Buygeneric meds…
Buy@Abilify.Online” rel=”nofollow”>..…
Buynow it…
Order@Coral.Calcium.Online” rel=”nofollow”>..…
Buyno prescription…
Buy@Cheap.Abilify” rel=”nofollow”>……
Buygeneric pills…
Order@Discount.Acai” rel=”nofollow”>..…
Buywithout prescription…
Purchase@Cheap.Acai” rel=”nofollow”>……
Buyit now…
Order@Cheap.Energy.Boost” rel=”nofollow”>.…
Buynow it…
Order@Accupril.Online” rel=”nofollow”>.…
Buygeneric drugs…
Order@Discount.Accupril” rel=”nofollow”>.…
Buygeneric drugs…
Order@Accutane.Online” rel=”nofollow”>.…
Buynow it…
Buy@Aciphex.Without.Prescription” rel=”nofollow”>.…
Buygeneric meds…
Cheap@Generic.Actonel.35mg” rel=”nofollow”>..…
Buygeneric meds qmo…
Order@Cheap.Actos” rel=”nofollow”>.…
Buygeneric drugs zfk…
Get@Actos.Online” rel=”nofollow”>..…
Buywithout prescription dyo…
Generic@Actos.15mg.30mg.Without.Prescription” rel=”nofollow”>..…
Buygeneric drugs krg…
Cheap@Coral.Calcium.Online” rel=”nofollow”>……
Buywithout prescription xiu…
Purchase@Generic.Abilify.5mg.10mg.15mg.20mg.30mg” rel=”nofollow”>..…
Buywithout prescription yen…
Order@Generic.Acai” rel=”nofollow”>..…
Buyno prescription wkp…
Cheap@Generic.Acai.500mg” rel=”nofollow”>..…
Buyno prescription tao…
Buy@Cheap.Accupril” rel=”nofollow”>.…
Buygeneric drugs nih…
Cheap@Accupril.Online” rel=”nofollow”>..…
Buygeneric drugs ajd…
Purchase@Discount.Accutane” rel=”nofollow”>.…
Buygeneric drugs mby…
Buy@Generic.Accutane” rel=”nofollow”>……
Buygeneric meds svb…
Purchase@Generic.Accutane” rel=”nofollow”>.…
Buygeneric drugs axm…
Cheap@Accutane.10mg.20mg” rel=”nofollow”>.…
Buyit now ojb…
Order@Aciphex.Online” rel=”nofollow”>.…
Buynow it lmg…
Cheap@Aciphex.Without.Prescription” rel=”nofollow”>……
Buyit now sge…
Purchase@Discount.Acomplia” rel=”nofollow”>.…
Buynow tqa…
Order@Actonel.Without.Prescription” rel=”nofollow”>..…
Buyno prescription jwi…
Buy@Discount.Advair” rel=”nofollow”>..…
Buydrugs without prescription faj…
Buy@Cheap.Aggrenox” rel=”nofollow”>..…
Buygeneric meds dxy…
Buy@Cheap.Aldactone” rel=”nofollow”>..…
Buydrugs without prescription hak…
can@you.take.aciphex.and.nexium.at.the.same.time” rel=”nofollow”>..…
Buygeneric pills…
actonel@and.heart.irregularities” rel=”nofollow”>..…
Buyno prescription…
aldactone@kidney.problems” rel=”nofollow”>……
Buygeneric drugs…
abilify@and.sleep.issues” rel=”nofollow”>..…
Buydrugs without prescription…
actonel@and.dvt” rel=”nofollow”>.…
Buygeneric meds…
aloe@vera.juice.for.sale” rel=”nofollow”>..…
Buyit now…
side@effects.of.aricept” rel=”nofollow”>..…
Buyit now…
medieval armour…
Buy_generic drugs…
levaquin 500mg…
Buy_no prescription…
urinary tract infection canine…
Buy_generic pills…
financial aid for alzheimer’s…
Buy_without prescription…
behavior symptoms of prescription drug addiction…
Buy_generic meds…
heart worm medication…
Buy_no prescription…
honey for diabetes…
Buy_generic meds…
warfarin sod…
Buy_drugs without prescription…
verapamil side effects…
Buy_drugs without prescription…
blood pressure medications…
Buy_now it…
death rate of hiv…
Buy_it now…
hepatitis b shot…
Buy_it now…
drug trafficking in latin america…
Buy_drugs without prescription…
trigger point injections with lidocaine…
Buy_without prescription…
steroids and weight gain…
Buy_generic pills…
adhd cant take stimulants adult…
Buy_it now…
dehli meats during pregnancy…
Buy_now it…
permanent removal of adult acne scars…
Buy_generic meds…
nicotrol inhaler buy free sample oregon…
Buy_now…
enhancement drugs…
Buy_no prescription…
imodium during pregnancy…
Buy_generic meds…
diabetes popcorn…
Buy_generic pills…
drug test company…
Buy_without prescription…
allergy blood testing…
Buy_it now…
atlanta zyprexa lawyers…
Buy_generic drugs…
social etiologies for patients with schizophrenia…
Buy_it now…
weight loss pa…
Buy_no prescription…
clinical pharmacists prescribe medication…
Buy_it now…
epson salt and lemon juice diet…
Buy_generic pills…
l91 energizer e2 lithium aa cells…
Buy_generic drugs…
how to buy glucose meter…
Buy_generic meds…
skin cancer foundation australia…
Buy_generic drugs…
clinical features of sickle cell disease…
Buy_generic drugs…
breast cancer awareness wristband…
Buy_generic meds…
social skills training negative symptoms schizophrenia…
Buy_generic meds…
school age adhd self absorbed…
Buy_now it…
drug testing passing short notice…
Buy_generic meds…
overweight employee abuse…
Buy_generic drugs…
liquid tylenol and dogs…
Buy_without prescription…
10 mg provera…
Buy_generic pills…
palpitations side effect homeo medications…
Buy_generic pills…
meal plan for hypertension…
Buy_generic drugs…
scabies in dogs help…
Buy_generic drugs…
john degan anna degan il…
Buy_drugs without prescription…
herbals for pregnancy…
Buy_drugs without prescription…
food allergy and adenoids…
Buy_without prescription…
esophagus 2009 jelsoft enterprises ltd…
Buy_now it…
diet secrets for models…
Buy_now it…
propecia@before.and.after.picture” rel=”nofollow”>.…
Buywithout prescription…
best@medicare.plan.d.for.nexium.40.mg” rel=”nofollow”>..…
Buygeneric meds…
infant@prevacid.dose” rel=”nofollow”>.…
Buygeneric drugs…