addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupsimageimagesinstagramlinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1outlookpersonJoin Group on CardStartprice-ribbonImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruseryahoo

Re: [nycruby] Ruby/Regex Question

From: Ethan
Sent on: Monday, May 21, 2012 3:24 AM
The idea of parsing a language with regexp is amiss from the start. 

Translating spaces away - what if they are inside a string? Modifying string literals is bad. If your next thought is to start detecting strings with your regexp, you are starting down a dark and evil path that leads nowhere good. 

This fellow says it well - it applies just as well to javascript as it does to HTML - in the first and highest-voted answer at

On Mon, May 21, 2012 at 2:25 AM, David H <[address removed]> wrote:
Hey Rubyists,

This is more of a regex inquiry, but it's in a Ruby context; I'm
working on porting* jsmin form C to a much smaller Ruby script and
using regex to do it. Currently, I'm attempting to do a conditional
replace of spaces according to Crockford's spec

"It omits spaces except when a space is preceded and followed by a
non-ASCII character or by an ASCII letter or digit, or by one of these
characters: \ $ _"

I figured this bit would take care of that:

compressed_js = uncompressed_js.gsub(/[^[\x20-\x7Ea-zA-Z0-9\$\\_]]
[^[\x20-\x7Ea-zA-Z0-9\$\\_]]/) {
   |match| match.gsub(/ /, "")

I suspect that the regex itself might be checking for an OR condition
instead of the AND described in the spec. Would anyone know if that or
something else may be amis?

* Porting the minification parts without the syntax checking parts,
and adding support for combining multiple .js files into a single
bundle of fun. Code-in-progress here:

Love and kittens,

david [.dh] huerta

Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by David H ([address removed]) from NYC Rubyist Roundtable.
To learn more about David H, visit his/her member profile:
Set my mailing list to email me

As they are sent

In one daily email

Don't send me mailing list messages
Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]

Our Sponsors

  • AlphaSights

    Generously hosts and provides food and beverages for the meetup.

  • ThoughtWorks

    Generously hosts and provides food and beverages for the meetup.

  • Pivotal Labs

    Pivotal Labs generously donates meeting space.

  • Case Commons

    Case Commons provides food and beverages for the meetup.

  • The Pragmatic Bookshelf

    The Pragmatic Bookshelf donates their top notch books to the meetup.

  • Magnetic

    Magnetic generously hosts Ruby Meetups.

  • O'Reilly Books

    Providing print resources from a variety of publishers

  • Crowdtap

    Generously hosts roundtable discussions.

  • SmartLogic

    SmartLogic makes sure we are neither hungry nor thirsty

  • Big Fuel


People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy