Posts

Showing posts with the label RegExp

Berlin JS - RegExp Slides

Here they are ! (published live) If you want to test examples remember to replace weird keynote double-quotes with normal one :) Enjoy JavaScript Regular Expressions

How To Map Your Code

Most of the tools we use on daily basis to develop applications with any kind of programming language need to understand our code in order to help us while we are writing, reading, or creating code. We open an editor, we read highlighted code, we like suggestions when we write a dot and we use compilers, minifiers, compressors, obfuscators, converters, documentation generators ... but have we ever though about the program behind our same program? Have we never thought that highlights, suggestions, minifications, and syntax analyzer do not come for free and these are primordial programs we've ever used often without even realizing we are using it? This post is about general suggestions, techniques, and practices, used to map code. In this specific case, we will analyze step by step the logic behind a code mapper using the most used programming language in the world: JavaScript . Map VS Tokens First of all we need to understand what we need. A Map could be considered a generic list...

Literal Regular Expression Safe Regular Expression

... sorry for the redundant title but that's exactly what is this post about ... after yesterday explanation about problem, logic, and solution, to grab valid strings inside JS code, here I am with the literal RegExp able to grab literals RegExps in a generic JavaScript code. Why Do Not Add Just A "/" Into Other Strings RegExp One comment gave me the hint to write this second post about RegExps. While time is a bit over during days, this answer is simple, but not obvious! Differences between strings and literal regular expressions are basically these: there must be at least one char, or the parser will consider the literal RegExp an inline comment // the slash does NOT necessary need to be escaped. If we have a slash inside a range [a/b] the latter one won't break the RegExp and the slash will be considered just one valid char in that range there could be one or more chars after, where i(ignore case), g(match all), and m(multi line) can be present one or more times L...

String Escape Safe Regular Expression

I should have probably investigated more but apparently I did it ... the most problematic I've encountered so far with JavaScript RegExp seems to be solved! Update Indeed, I should have investigated ... I just like to find solutions by my own. I am not surprised somebody already investigated this classic parsing problem. Steve talked about it a year ago, using the lookbehind missed feature I talked in this post. Above post has much more details than mine (and much more Edits as well). The good part I am happy about is that both me and Steve came out with basically the same solution, but His one is definitively more compact: // Steves Levithan compact solution /(["'])(?:(?=(\\?))\2.)*?\1/g The assumption of above regexp is that if there is a char followed by an escape one, there must be another char that cannot be the initial single or double quote, being the latter one outside the second un captured part, and after a non greedy operation. If the second condition, \2, does...