Skip to content

Instantly share code, notes, and snippets.

@flogvit
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flogvit/7b32fe95a44d7b470c63 to your computer and use it in GitHub Desktop.
Save flogvit/7b32fe95a44d7b470c63 to your computer and use it in GitHub Desktop.
Little regexp magic to parse params
var text = 'Little brown="and yellow" fox=1 jumps over=lazy dog';
var res = text.match(/([^=\s]+="[^"]+")|\S+/g).map(function (p) {
return p.match(/^([^=\s]*)(?:="?([^"]*)"?)?$/).splice(1, 2);
})
console.log(res);
// Output
res =
[ [ 'Little', undefined ],
[ 'brown', 'and yellow' ],
[ 'fox', '1' ],
[ 'jumps', undefined ],
[ 'over', 'lazy' ],
[ 'dog', undefined ] ]
// If you are going to use the code, I suggest using underscores map function to handle null results easier:
var _ = require('underscore');
var res = _.map(text.match(/([^=\s]+="[^"]+")|\S+/g), function (p) {
return p.match(/^([^=\s]*)(?:="?([^"]*)"?)?$/).splice(1, 2);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment