Skip to content

Instantly share code, notes, and snippets.

@anhang
Created August 3, 2011 22:47
Show Gist options
  • Select an option

  • Save anhang/1124043 to your computer and use it in GitHub Desktop.

Select an option

Save anhang/1124043 to your computer and use it in GitHub Desktop.

Revisions

  1. anhang revised this gist Aug 4, 2011. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions html_parse_params
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,9 @@

    function parseParams(str){
    return JSON.parse("{"+
    str.replace(/.+?=.+?(,|$)/g,
    function(part){ return part.replace(/(.+)(=)([^,]+)(,*)/,
    function(x, key, eq, val, com){return ["\"",key,"\"",":","\"",val,"\"",com].join('');})
    str.replace(/.+?=.+?(&|$)/g,
    function(part){ return part.replace(/(.+)(=)([^&]+)(&*)/,
    function(x, key, eq, val, com){return ["\"",key,"\"",":","\"",val,"\"",(com ? "," : '')].join('');})
    }) +"}");
    }

  2. anhang revised this gist Aug 3, 2011. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions html_parse_params
    Original file line number Diff line number Diff line change
    @@ -6,8 +6,7 @@ function parseParams(str){
    str.replace(/.+?=.+?(,|$)/g,
    function(part){ return part.replace(/(.+)(=)([^,]+)(,*)/,
    function(x, key, eq, val, com){return ["\"",key,"\"",":","\"",val,"\"",com].join('');})
    }) +"}";
    );
    }) +"}");
    }

    // str = "id=MY_ID,title=MY_TITLE"
  3. anhang created this gist Aug 3, 2011.
    18 changes: 18 additions & 0 deletions html_parse_params
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // Super basic html parser written with regex.
    // Experimental and untested

    function parseParams(str){
    return JSON.parse("{"+
    str.replace(/.+?=.+?(,|$)/g,
    function(part){ return part.replace(/(.+)(=)([^,]+)(,*)/,
    function(x, key, eq, val, com){return ["\"",key,"\"",":","\"",val,"\"",com].join('');})
    }) +"}";
    );
    }

    // str = "id=MY_ID,title=MY_TITLE"
    // parseParams(str)
    // Object
    // id: "MY_ID"
    // title: "MY_TITLE"
    // __proto__: Object