Last active
August 29, 2015 14:23
-
-
Save flogvit/7b32fe95a44d7b470c63 to your computer and use it in GitHub Desktop.
Little regexp magic to parse params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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