3 ms·
I appreciate your library, that's pretty cool. I just want to point out that some (most?) regex libraries support a whitespace-insensitive mode, which allows y
by benley 10y ago
I appreciate your library, that's pretty cool. I just want to point out that some (most?) regex libraries support a whitespace-insensitive mode, which allows you to write out the raw regex in a way that's considerably easier for humans to visually grok:
(?x)
[(),]
| "(?: \\. | [^"\n] )*"
| '(?: \\. | [^'\n] )*'
| \/ \* [\s\S]*? \* \/
That works in python, at least; I don't really know javascript so I can't speak to that.
- TeMPOraL 10y agoIn Java I prefer to do something like this: "[(),]" // match the foo part... + "|\"(?:\\.|[^\"\n])*\"" //... or, match the bar part in *double* quotes, putting quoted value in capture group 1... + "|'(?:\\.|[^'\\n])*'" //... or, match the bar part in *single* quotes, putting quoted value in capture group 1... + "|\/\*[\s\S]*?\*\" //... or, match whatever the hell that is. Simple string splitting + commenting the semantic parts. Also, labeling the capture group (and creating named constants for them in your code next to your regex) is a huge win.
- pygy_ 10y agoIf the string you happen to match contains a lot of metacharacters, you end up with backslashes all over the place, which makes the result hard to read. Nested groups and captures are also often hard to parse. FWIW, you forgot to double escape `\\\\.`, and didn't close the CSS comment (last alternative). "[(),]" // match the foo part... + "|\"(?:\\\\.|[^\"\\n])*\"" //... or, match the bar part in *double* quotes + "|'(?:\\\\.|[^'\\n])*'" //... or, match the bar part in *single* quotes + "|\/\*[\s\S]*?\*\/" // or match the comment Also, you're probably not familiar with the quirks of JS regexps, but the two string alternatives use non-capturing groups, and `[\s\S]` is the true "any" matcher, `.` doesn't match new lines. At last, `*?` is a non-greedy `*`. (Edited thrice, damn you italics).
- TeMPOraL 10y agoThanks for the clarifications! I admit I just copied your example and tried to sort-of convert it into Java style. It definitely won't be a correct Java-compatibile regex. As you said, I'm not familiar with the JS regex quirks. I just couldn't invent a good example on the spot, and didn't want to post ones from the code I work on at my day job for legal reasons. And yeah, I agree about "lots of backslashes" part. It gets messy - but splitting regexps in parts makes it at least more manageable. I'm not yet angry enough at the cases I have at my day job to whip up a DSL for it though.
- pygy_ 10y agoNo, JS doesn't have whitespace-insensitive RegExps, which is why I wrote the lib. CoffeeScript does but it's losing ground for unrelated reasons.
- gmac 10y agoThis is the 'extended' syntax. It's not supported out of the box in Javascript, but it's not hard to add: http://blog.mackerron.com/2010/08/08/extended-multi-line-js-regexps/ http://blog.mackerron.com/2010/08/08/extended-multi-line-js-...
- pygy_ 10y agoGood one. The biggest issue was the lack of multi-line strings in JS, but ES6 adoption is about to solve that. Would you mind publishing it on NPM (or at least tacking a license to it)?