admin管理员组

文章数量:1332383

I have an array of people and want to filter on some criteria, in this case I want to search for a "male" which should return just one result however with this set I'm getting everything back because the regular expression is capturing female as well.

I tried matching on "\bmale" or "/\bmale" but didn't get anything back. I know I need to use a word boundary but for some reason it's not working out.

var people = [["Adelaide", 2, "yes", "female"],
           ["Ada", 2, "yes", "female"],
           ["Amanda", 1, "yes", "female"],
           ["Wolf", 3, "no", "male"],
           ["Rhonda", 1, "no", "female"]];

var isMale = function(x) {
  var myMatch = new RegExp("male");
  var test = String(x).match(myMatch);
  return String(x).match(myMatch);
}


var filteredArray=people.filter(isMale);

document.writeln(filteredArray); 

I have an array of people and want to filter on some criteria, in this case I want to search for a "male" which should return just one result however with this set I'm getting everything back because the regular expression is capturing female as well.

I tried matching on "\bmale" or "/\bmale" but didn't get anything back. I know I need to use a word boundary but for some reason it's not working out.

var people = [["Adelaide", 2, "yes", "female"],
           ["Ada", 2, "yes", "female"],
           ["Amanda", 1, "yes", "female"],
           ["Wolf", 3, "no", "male"],
           ["Rhonda", 1, "no", "female"]];

var isMale = function(x) {
  var myMatch = new RegExp("male");
  var test = String(x).match(myMatch);
  return String(x).match(myMatch);
}


var filteredArray=people.filter(isMale);

document.writeln(filteredArray); 
Share Improve this question asked Sep 7, 2011 at 0:23 firedrawndaggerfiredrawndagger 3,73310 gold badges36 silver badges51 bronze badges 4
  • 3 Just remind me one more time: Why are you using a Regex for this? – Joseph Silber Commented Sep 7, 2011 at 0:24
  • This could be done much more simply: var filteredArray = people.filter(function(itm) { return itm[2] == "male"; }); – Alxandr Commented Sep 7, 2011 at 0:27
  • @Joseph Silber - because it's convenient to filter a multidimensional array since that's what a match() returns. Unless there's an easier way? – firedrawndagger Commented Sep 7, 2011 at 0:30
  • @firedrawndagger - take a look at my answer, then e back here. – Joseph Silber Commented Sep 7, 2011 at 0:31
Add a ment  | 

5 Answers 5

Reset to default 3

Don't use a regular expression when it's not necessary!!!

Use this instead:

var people = [
    ["Adelaide", 2, "yes", "female"],
    ["Ada", 2, "yes", "female"],
    ["Amanda", 1, "yes", "female"],
    ["Wolf", 3, "no", "male"],
    ["Rhonda", 1, "no", "female"]
];

var filteredArray = people.filter(function(el){
    return el[3] == 'male';
});

http://jsfiddle/XRHtZ/ (check your console)

Why not just match against ^male, which would be the start of the line, immediately followed by "male"?

The word "female" contains the word "male" :-)

You filter should really just look at the simple value of the last element in the ponent sub-arrays. No need to use a regex; just pare:

return  x[3] === "male";

That's really all your "isMale()" function needs to do.

In my opinion, the beginning/end of string characters would be more appropriate here:

var myMatch = new RegExp('^male$');

If you want to only match items that are exactly "male", then just pare directly:

function isMale(x) {
    return(x == "male");
}

No need to use a regular expression for an exact match.

本文标签: regexReturning an exact match from a JavaScript array using RegExpStack Overflow