Skip to content

Instantly share code, notes, and snippets.

@hiz8
Last active October 25, 2015 23:47
Show Gist options
  • Select an option

  • Save hiz8/ae07b72919c702e7eaef to your computer and use it in GitHub Desktop.

Select an option

Save hiz8/ae07b72919c702e7eaef to your computer and use it in GitHub Desktop.
Get query string values in ES6.

Get query string values in ES6.

class GetParams {
  constructor(name) {
    this.name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  }

  run() {
    const regex   = new RegExp("[\\?&]" + this.name + "=([^&#]*)"),
          results = regex.exec(location.search);

    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  }
}
  
const getParams = new GetParams('hash'),
      hash = getParams.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment