0

I have some string with the hash, after which I need get plantype=chine to satisfied the condition. when I replace the # to & it works fine, but I need to use #

  var url = href='//somewebsite/index.html?plan=usa#plantype=china';

  var queryString = {};
  url.replace(
    new RegExp("([^?=&]+)(=([^&]*))?", "g"),
    function($0, $1, $2, $3) { 
      queryString[$1] = $3; 
    }
  );

  if (queryString['plantype'] == 'china') {
   // the condition is not satisfied when I use # in a string
  }

How to fix it, and what I'm doing wrong?

2 Answers 2

1

Try

var queryString = {}
, url = 'href=//somewebsite/index.html?plan=usa#plantype=china'
, url = url.split("#")[1].split("=");
queryString[url[0]] = url[1];
if (queryString["plantype"] === "china") {
  console.log(queryString)
}

var queryString = {}
, url = 'href=//somewebsite/index.html?plan=usa#plantype=china'
, url = url.split("#")[1].split("=");
queryString[url[0]] = url[1];
if (queryString["plantype"] === "china") {
  console.log(queryString)
}

Sign up to request clarification or add additional context in comments.

Comments

0

You must check for # in the regular expression, replace & by #

new RegExp("([^?=#]+)(=([^#]*))?", "g"),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.