最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to use string match() with angularJs $scope.search variable - Stack Overflow

matteradmin9PV0评论

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

Post a comment

comment list (0)

  1. No comments so far