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
5 Answers
Reset to default 3Don'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
版权声明:本文标题:regex - Returning an exact match from a JavaScript array using RegExp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742294410a2448431.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论