-3

I want to get the page number out if these possible strings:

$scope.projects.next can equal...

  • string 1) /api/project/?currentuser=false&page=2
  • string 2) /api/project/?page=2

I have tried this:

$scope.projects.next.split('=')[1].indexOf("page")

However, this only works on string 2. How can I change this to make sure it finds the page number no matter what position or future arguments that could be added?

I have also tried:

$scope.projects.next.indexOf("page")

but this gives 52 no idea why.

Prometheus
  • 32,405
  • 54
  • 166
  • 302
  • because `indexOf` returns the starting character index in the string that the passed argument starts at – Patrick Evans Aug 13 '14 at 11:08
  • 1
    Why split at all? just find index of `page=` and there it is. – pawel Aug 13 '14 at 11:08
  • 1
    `split('page=')` will probably work, but you'll still have a problem if that param/value pair don't come at the end of your string. You're probably better off using a regex to find the `\d+` after 'page=` – vch Aug 13 '14 at 11:08
  • @Shiju K Babu its a string that represents a URL. – Prometheus Aug 13 '14 at 11:11
  • @Spike Is that a URL? Then you can use `$routeParams` if that's just a String value only, you need to use RegEx or something. – Coder Aug 13 '14 at 11:11
  • The indexOf function returns the position of the data specified in the parentheses. For example "hello".indexOf('e') will return 1 since "e" occurs at the second position of the string. – Jeremy Karlsson Aug 13 '14 at 11:12

5 Answers5

5

You can use regex:

/page=([0-9]+)/.exec($scope.projects.next)[1]

That uses a regular expression with a capture group (the bit in ()) for the page number, and captures one or more digits after page=. The result is an array, where the first element is the whole match, and the second is the capture group (that's why we have [1] at the end).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Thiago Barcala
  • 6,463
  • 2
  • 20
  • 23
  • 2
    +1 - It's always best to *explain* what you've done as well as showing the code for doing it. I've added that bit, but it's a good habit to get into. – T.J. Crowder Aug 13 '14 at 11:13
  • ok this works well, tested it with a few different arguments and always seems to work. – Prometheus Aug 13 '14 at 11:16
3

please see here: http://jsbin.com/gizalo/1/edit

function to get parametr from string

function getParameterByName(name, string) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(string);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

Controller

app.controller('firstCtrl', function($scope){

 var stringa="/api/project/?currentuser=false&page=2";
 var stringb="/api/project/?page=2";

 $scope.a = getParameterByName('page', stringa);
 $scope.b = getParameterByName('page', stringb);
});
sylwester
  • 16,498
  • 1
  • 25
  • 33
1

The problem is that in the first url there are two = signs so your index would be out by 1 on the first url

$scope.projects.next.split('page=')[1]

Andrew
  • 5,215
  • 1
  • 23
  • 42
0

This has absolutely nothing to do with AngularJS. But it's also simple, so why not answer. Splitting on a character splits on EVERY instance of that character. Your top string is going to end up being ['/api/project/?currentuser', 'false&page', '2'], so getting the position of [1]('page') isn't useful. Split it on ? instead.

Chad Robinson
  • 4,575
  • 22
  • 27
  • you would then need to split again on & and then on = – Patrick Evans Aug 13 '14 at 11:15
  • Split on ? then take the 2nd element of the resulting array and split on & and then loop through the array until you find a string that contains 'page=' and split on = and then take the 2nd element of that array – Andrew Aug 13 '14 at 11:15
0
var start = $scope.projects.next.indexOf("page=");
var end   = $scope.projects.next.indexOf("&");

var page;

if(end == -1) {
    page = $scope.projects.next.substring(start+5);
} else {
    page = $scope.projects.next.substring(start+5, end);
}

This would also work for ?page=17&test=2

Marcel Burkhard
  • 3,453
  • 1
  • 29
  • 35