RegExp: Matching Balanced Parenthesis and Quotes (greedy, non-recursive)

Solution

I need to match all the text within balanced parens, single quotes, or double quotes, but don’t snag on the content within, i.e. be greedy! “Balanced” means there should be a corresponding single or double quote, but not mix them, or there should be a closing paren for each opening paren.

PCRE:

The match will be in capturing group #3.

In short, this regular expression uses a positive lookahead (?=...)  within the lookaround conditional  (?...)  and the backreference \2  to match the initiating character. If it is a single quote or double quote, it was contained within \2 , but if it was an open paren ( , it is not contained in a capture group and therefore it should match a close paren. In retrospect, this could be enhanced, and also needs some tweaks for JavaScript compatibility since lookaround conditionals are not supported.

Do note that this matches the beginning and ending of the string ^$ , so may require tweaking for other applications.

Test Cases

regex101 link: https://regex101.com/r/fG4rZ8/3

Matches:

These will match hello , hello world , or in some cases 'hello world  for the last test case.

Doesn’t Match:

Node.js: Dynamic Event Listener Binding and Removal

Read this SO question first: Removing event listener which was added with bind

Now that we’re on the same page. I’m going to make this short’n’sweet by providing a premise and letting the code do the talking: The example provided in the SO post was all fine and dandy, but was for the case of single-use. I needed a way to clean up event listeners on a Node.js net.Server and associated socket connections.

 

 

Basic PHP Class Serialization

Just a quick brain dump regarding serializing classes in PHP. There is nothing advanced about this, and TBH I haven’t even read up on the caveats of serializing classes in PHP – perhaps a task for another day.

Judging from the string output after serializing the class, it appears that it is simply a named object. When unserializing, the class must be defined to unserialize completely, otherwise you’re stuck with an instance of __PHP_Incomplete_class. Hint: when in the same file, class definitions work just like function definitions, i.e. you can put them anywhere, before or after target code.

Notice the Fatal Error above. This example didn’t follow my instructions of having the class “X” defined. To remedy this, simply:

And now, with both PHP snippets merged into a single file, this is what the output looks like:

 

“Falsy” vs “Falsey” – I’m Making the Call

This is probably the most asinine blog post I’ll write, but for my sanity, it must be done.

It is spelled falsy.

Without the “e” between the “s” and the “y” – priod, th nd.

Okay. So what is Falsy anyway?

In loose-typed languages like PHP or JavaScript values can be considered Boolean false if they are '' (empty strings), the number 0 (zero), NULL, and most obviously Boolean false itself, just to name a few (in PHP an empty array is considered false, while in JavaScript it is not). It is important to note that this is only in the context of non-strict comparison, i.e. == and not  ===, and when type casting to Boolean (see PHP’s type juggling reference).

When describing a false-like value it is easier/faster to type “falsy” (or the now incorrect way of “falsey”) since it has less keystrokes, and it is easier to read due to the lack of character bloat; incidentally it also fits nicely as a variable because there is no hyphen (devil’s advocate: camelCase) and it is not a reserved word. It is important that the developer does not simply describe the value as “false” since this implies a variable of type Boolean, however using “falsy” implies a false-like value of any data type which, albeit more ambiguous in its definition, is more descriptive in its context.

Supporting Data

Let’s let the data speak for a moment. After analyzing a Google Trends chart of “falsy” vs “falsey,” the results are initially a bit misleading. “Falsey” is the clear winner at first glance, only to find that there is a name and published articles with the name “Falsey.” Considering that “falsy” is not trailing by many datapoints provides the confidence that it is in use and very applicable. Most importantly in regards to the related searches, “falsy” prevails over “falsey” which does “not [have] enough search volume to show results”: “javascript falsy” and “falsy values.”

Addition: Douglas Crockford even uses “falsy” in his writing, “The Elements of JavaScript Style.” Perhaps I didn’t need to write this post after all, but I sure hope it solidifies the answer indefinitely and provides a resource for any other concerned developers out there.

Spelling Review

I am by no means an English major, or anywhere near one, but I do have a knack for research and comprehension.

With regard to the suffixation of the letter “y” (vowel) when transforming a word, the “e” is dropped per the rules:

In the word “false,” the word ends with the vowel “e,” is preceded by the consonant “s,” and none of the exceptions are applicable.

F – A – L – S – Y

Node.js: Handling File Uploads without Express

I was working on a little project which does not utilize express. Part of the program needed to handle file uploads. In PHP this is easy with the $_FILES superglobal, and with express this is also just as easy with req.files. In pure node.js the developer doesn’t have this luxury because they’re just dealing with raw http requests and responses (I’m surprised node.js even parses the headers, it is a miracle!). Before I begin, this whole process should be optimally streamed to a file so large file uploads do not consume more RAM than their response chunk size, but I didn’t do this, shame on me.

First, capture the response body. Here I am assuming you have a HTTP server that is listening and responding to requests:

Here, I’ll give you the entire source to my MultipartParser Node.js module. It requires nodeproxy.

And the implementation is as follows:

LittleDiddy does not have any path routing (e.g. it won’t actually show a form that uses enctype="multipart/form-data"), it simply accepts and parses a POST with “file” inputs. This “little diddy” (implementation example) is untested by the way, so good luck.

This article’s source is released under the MIT license; I don’t care what you do with it.

Adobe Shadow

This is probably the greatest thing since sliced bread: Adobe Shadow (Sd).

In summary, it synchronizes your desktop (PC and Mac) browser with your hand-held touch device (both Android and iOS) for streamlined mobile design, development, and troubleshooting. You can get up and running in 3 easy steps:

  1. Download Adobe Shadow from Adobe Labs
  2. Download the app on your mobile device
  3. Download the Google Chrome Browser extension

I haven’t used it yet, but will give it a whirl within the next few days… and probably post my experience.