admin管理员组

文章数量:1025278

I try to use string match with ignore case sensitivity and my input $scope.search variable, but it doesn't work.

<input type="text" ng-model="search" autofocus="autofocus">




angular.forEach(groups, function(group, key) {

   console.log(group); // => Object contains

    if (group.label.match(/($scope.search)/i)) {
      result[key] = group;
    }

});

Data Object group:

Object { label: "Transfiguration", articles: Array[1] }

How to use group.lable.match() with $scope.search correctly?

Many thanks.

I try to use string match with ignore case sensitivity and my input $scope.search variable, but it doesn't work.

<input type="text" ng-model="search" autofocus="autofocus">




angular.forEach(groups, function(group, key) {

   console.log(group); // => Object contains

    if (group.label.match(/($scope.search)/i)) {
      result[key] = group;
    }

});

Data Object group:

Object { label: "Transfiguration", articles: Array[1] }

How to use group.lable.match() with $scope.search correctly?

Many thanks.

Share Improve this question edited Jan 5, 2015 at 14:05 asked Jan 5, 2015 at 13:52 user4420255user4420255
Add a ment  | 

2 Answers 2

Reset to default 1

Your regular expression is dynamic so you cannot use /regexp/ syntax. You need to create a Regexp object instead.

angular.forEach(groups, function(group, key) {
    console.log(group); // => Object contains

    if (group.label.match(new RegExp("(" + $scope.search + ")", "i"))) {
      result[key] = group;
    }
});

You can probably remove the brackets from the RegExp too.

It is better also to use test() because you aren't interested in the results:

if(new RegExp($scope.search, "i").test(group.label)) {

Finally, if this is a basic search, putting both parts to lower case and using indexOf should be more efficient:

if (group.label.toLowerCase().indexOf($scope.search.toLowerCase()) > -1) {

you can always use javascript in angularjs modules :)

angular.forEach(groups, function(group, key) {

   console.log(group); // => Object contains

    if (group.label.toLowerCase().indexOf($scope.search.toLowerCase())!=-1) {
      result[key] = group;
    }

});

This should solve your problem

I try to use string match with ignore case sensitivity and my input $scope.search variable, but it doesn't work.

<input type="text" ng-model="search" autofocus="autofocus">




angular.forEach(groups, function(group, key) {

   console.log(group); // => Object contains

    if (group.label.match(/($scope.search)/i)) {
      result[key] = group;
    }

});

Data Object group:

Object { label: "Transfiguration", articles: Array[1] }

How to use group.lable.match() with $scope.search correctly?

Many thanks.

I try to use string match with ignore case sensitivity and my input $scope.search variable, but it doesn't work.

<input type="text" ng-model="search" autofocus="autofocus">




angular.forEach(groups, function(group, key) {

   console.log(group); // => Object contains

    if (group.label.match(/($scope.search)/i)) {
      result[key] = group;
    }

});

Data Object group:

Object { label: "Transfiguration", articles: Array[1] }

How to use group.lable.match() with $scope.search correctly?

Many thanks.

Share Improve this question edited Jan 5, 2015 at 14:05 asked Jan 5, 2015 at 13:52 user4420255user4420255
Add a ment  | 

2 Answers 2

Reset to default 1

Your regular expression is dynamic so you cannot use /regexp/ syntax. You need to create a Regexp object instead.

angular.forEach(groups, function(group, key) {
    console.log(group); // => Object contains

    if (group.label.match(new RegExp("(" + $scope.search + ")", "i"))) {
      result[key] = group;
    }
});

You can probably remove the brackets from the RegExp too.

It is better also to use test() because you aren't interested in the results:

if(new RegExp($scope.search, "i").test(group.label)) {

Finally, if this is a basic search, putting both parts to lower case and using indexOf should be more efficient:

if (group.label.toLowerCase().indexOf($scope.search.toLowerCase()) > -1) {

you can always use javascript in angularjs modules :)

angular.forEach(groups, function(group, key) {

   console.log(group); // => Object contains

    if (group.label.toLowerCase().indexOf($scope.search.toLowerCase())!=-1) {
      result[key] = group;
    }

});

This should solve your problem

本文标签: javascriptHow to use string match() with angularJs scopesearch variableStack Overflow