I recently learned that performance in for loops could be greatly increased by moving the length
/ count
outside the loop and thought I would take a look at implementing that throughout my site.
Doing a full regex search using for\(.*<.*\.length
showed that I had about 600 matches across 70 of the 12,000 files searched and as I was going through, I realized I was just cutting and pasting over and over with little variation where I would just copy the original code that calculated length, remove the dots and capitalize the first char.
For example: for(c=a.children.length-1;0<=c;c--)d
would become
var AChildrenLength=a.children.length;
for(c=AChildrenLength-1;0<=c;c--)d
Once I realized what I was doing was monotonous, it seemed time for some fun with regular expressions which could be turned into a shell script that could modify regular or minified files.
Seemed simple enough to get started, but then I ran into some trouble with some of the matches that were separated by dots. Here are some examples of ones I am having a hard time matching:
for($j = 0; $j < incomingData.customer['data'].length; $j++){
for(c=a.children.length-1;0<=c;c--)d
for(l=0;l<g.length;l++){d=g[l]
// this is a fun one to look at, I didn't know you could do a for loop like that.
for(h in b)if(!h.match(d||/margin|text\-align|width|border|padding/i))if(g)g=!1;else{var l=new CKEDITOR.htmlParser.element("span");l.attributes.style=h+":"+b[h];c.add(l);c=l;delete b[h]}CKEDITOR.tools.isEmpty(b)?delete a.attributes.style:a.attributes.style=CKEDITOR.tools.writeCssText(b);for(b=0;b<e.length;b++)c.add(e[b])},sortStyles:function(a){for(var b=["border","border-bottom","font-size","background"],c=m.parseCssText(a.attributes.style),d=m.objectKeys(c),e=[],g=[],h=0;h<d.length;h++)
for(var f=0,n=0,l=0,p,e=a.$.rows.length;l<e;l++)
for(var i = 0; i < this.audioLayerControl.listOfSequenceEditors.length; ++i)
I am using this regex: (?:for\(.*<\W?)(([\w]+)\.?)+(?:\[?)|(?:\.length)
You can see what is matched color coded here: https://regex101.com/r/GsPieq/2
For some reason it is only matching the last word in each. Can someone help me understand why it isn't working? I need it to match between 1 and probably 5 words, separated by dots, not include the .length
or anything in the brackets like {"data"]
where I can then loop through each match transforming the first char to capital.
It should not match anything that has already been properly formed such as for(c=AChildrenLength-1;0<=c;c--)d
Here was my thought process:
(?:for\(.*<\W?) // Non capture but match any for loop up to the test
(
([\w]+)\.? // capture any alphanumeric char up to a dot if it exists
)+ // repeat if possible
(?:\[?)|(?:\.length) // non capture find where it ends