admin管理员组文章数量:1427538
Say I have something like this:
[
[ 'Friend' ],
[ 'Friendship-' ],
[ 'Friends' ],
[ 'friendly' ],
[ 'friendster' ],
]
I want to go through this and find the field that matches "Friend", but Friend only - not Friends, Friendship etc. How would I do this?
I have tried indexOf() & regex match, but I am still a beginner so it always matches them all right now.
Say I have something like this:
[
[ 'Friend' ],
[ 'Friendship-' ],
[ 'Friends' ],
[ 'friendly' ],
[ 'friendster' ],
]
I want to go through this and find the field that matches "Friend", but Friend only - not Friends, Friendship etc. How would I do this?
I have tried indexOf() & regex match, but I am still a beginner so it always matches them all right now.
Share Improve this question edited Nov 15, 2018 at 19:51 R. Kohlisch asked Nov 15, 2018 at 18:35 R. KohlischR. Kohlisch 3,0137 gold badges33 silver badges64 bronze badges 2- What is that data structure you are starting with. Should that all be enclosed in a larger array that you want to filter? Is it a string? It's helpful if your examples can be copied and pasted without syntax errors. – Mark Commented Nov 15, 2018 at 18:37
-
1
try regex
^[F|f]riend$
– Matt.G Commented Nov 15, 2018 at 18:38
4 Answers
Reset to default 2To find the index you can use findIndex()
If you might have more than one to find, you can use filter()
which will return a list of matches. Since you want to match Friend
and only Friend
you can just use equality ===
to test.
Here's examples of both findIndex
and filter()
:
let arr = [
[ 'Friendship-' ],
[ 'Friends' ],
[ 'Friend' ],
[ 'friendly' ],
[ 'friendster', 'Friend' ]
]
// get first matching index
let idx = arr.findIndex(item => item.includes('Friend'))
console.log("Found Friend at index:", idx) // will be -1 if not found
// filter for `Friends`
let found = arr.filter(i => i.includes('Friend'))
console.log("All with friend: ", found)
There are a few good ways to solve this problem, since you mentioned RegExp This example will use a regular expression and the test
method (which returns a boolean). the ^
at the head of the expression tells the evaluator to only look in cases where the line starts with the expression. The $
does the same for the end. Combined you get a line which exactly matches your search criteria.
const arr = [
[ 'Friendship-' ],
[ 'Friends' ],
[ 'friendly' ],
[ 'Friend' ],
[ 'friendster' ],
]
const index = arr.findIndex(([val]) => /^Friend$/.test(val));
if (index === -1) {
console.log('No Match Found');
return;
}
console.log(arr[index]);
If your data structure is an array of arrays with a single item of a string, you could use filter:
let items = [
['Friend'],
['Friendship-'],
['Friends'],
['friendly'],
['friendster']
];
items = items.filter(x => x[0] === "Friend");
console.log(items);
let say you have
let a = [ [ 'Friend' ], [ 'Friendship-' ], [ 'Friends' ], [ 'friendly' ], [ 'friendster' ] ]
then:
let find = (a,w)=>{let i=-1; a.map((x,j)=>{if(x[0]===w) i=j}); return i}
let index = find(a, 'Friend'); // result: 0
if not found then find
returns -1
UPDATE
And here is shorter version based on Mark Meyer answer:
var find = (a,w)=>a.findIndex(e=>e[0]===w); // find(a, 'Friend') -> 0
let a = [ [ 'Friend' ], [ 'Friendship-' ], [ 'Friends' ], [ 'friendly' ], [ 'friendster' ] ]
var find = (a,w)=>a.findIndex(e=>e[0]===w);
console.log( find(a, 'Friend') );
本文标签: javascriptMatching exact string with indexOfStack Overflow
版权声明:本文标题:javascript - Matching exact string with indexOf - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745502450a2661093.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论