Skip to content

Instantly share code, notes, and snippets.

@flogvit flogvit/paramparser.js
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.