I pass in the address of the site pages to variable:

var page = location; var pageHref = page.toString(); 

Addresses are:

 http://mysite.ru/?page=home http://mysite.ru/?page=about http://mysite.ru/?page=news и т.д. 

How to pull out of similar lines and transfer to a variable what comes after the '=' sign?

The functions substr and substring, as I understand it, work with the positions of the substring in the string. And I need not the number of the character, starting from which the substring is cut, but the character itself, after which.

  • in the line there can be only one character = ? - Grundy
  • @Grundy, yes, only one - humster_spb

2 answers 2

If you need to get a substring after the last = , break the line with = and take the last element with pop() :

 var s = 'http://mysite.ru/?page=home'; var res = s.split('=').pop(); console.log(res); 

    The functions substring and substr are quite similar in this task. They just need to pass the index of the first character after the = sign. To determine the index, you can use the lastIndexOf function, which returns the index of the last occurrence:

     var s = 'http://mysite.ru/?page=home'; var res = s.substring(s.lastIndexOf('=') + 1); console.log(res); var res = s.substr(s.lastIndexOf('=') + 1); console.log(res);