Google 如果字符串中的任何单词与另一个字符串匹配,Apps 脚本会获取索引?
Google Apps Script to obtain the index if any of the words on a string match another string?
我有一长串从 sheet 范围中获得的角色,这些范围存储为数组中的字符串,举个例子,数组看起来像这样:
arr1 = ["football manager","hockey coach", "fb player","fb coach","footballer"];
我还有另一个数组,其中有一小部分标签
arr2 = ["football","fb", "footballer","hockey","rugby"];
我正在尝试将第一个数组的角色与第二个数组中的标签相匹配。
我一直在尝试通过循环获取匹配行的索引来做到这一点:
for(let i in arr1){
arr2.findIndex(s => s.indexOf(arr1[i]) >= 0);
}
但这只适用于“footballer”,因为它是完全匹配,我需要对所有部分匹配进行分类。
使用以下函数查找与 arr1
.
中的值匹配的标签索引(来自 arr2
数组)
详细解释请关注代码注释。
function matchTagIndexes()
{
// TODO replace with your values
arr1 = ["football manager","hockey coach", "fb player","fb coach","footballer"];
// TODO replace with your tags
arr2 = ["football","fb", "footballer","hockey","rugby"];
// for all tags create regex objects
// regex searches for any match that have `tag` surrounded with word (\b) boundaries
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet#boundary-type_assertions
const arr2Regexes = arr2.map(tag => new RegExp(`\b${tag}\b`, 'i'));
// loop arr1 values as val
arr1.map(val =>
// for each arr2 regex match val
arr2Regexes.forEach((regex, i) =>
// if it is matched, log value from arr1 array, matched tag name and tag's index in arr2 array
val.match(regex) && console.log(`"${val}" matches tag "${arr2[i]}" which has index ${i}`)
)
);
}
结果:
time
status
message
8:46:35 PM
Notice
Execution started
8:46:35 PM
Info
"football manager" matches tag "football" which has index 0
8:46:35 PM
Info
"hockey coach" matches tag "hockey" which has index 3
8:46:35 PM
Info
"fb player" matches tag "fb" which has index 1
8:46:35 PM
Info
"fb coach" matches tag "fb" which has index 1
8:46:35 PM
Info
"footballer" matches tag "footballer" which has index 2
8:46:36 PM
Notice
Execution completed
参考:
我怀疑某些文本可能有多个标签 (arr1
)。这是获取每个文本的标签(索引)数组的解决方案:
var texts = ['football manager','hockey coach', 'fb player','fb coach','footballer', 'none'];
var tags = ['football','fb', 'footballer','hockey','rugby', 'coach'];
// get all tags for all the texts
var list = [];
for (let tag of tags) {
var mask = RegExp('\b' + tag + '\b', 'i');
for (let text of texts) {
if (text.match(mask))
list.push( {'text': text, 'tag': tag, 'tag_index': tags.indexOf(tag)} );
}
}
console.log(list);
// group tags for the same texts
var text_and_tags = {};
for (let element of list) {
try { text_and_tags[element.text].push(element.tag_index) }
catch(e) { text_and_tags[element.text] = [element.tag_index] }
}
console.log(text_and_tags);
它将为您获取对象 text_and_tags
,如下所示:
{
'football manager': [ 0 ],
'fb player': [ 1 ],
'fb coach': [ 1, 5 ],
'footballer': [ 2 ],
'hockey coach': [ 3, 5 ]
}
我有一长串从 sheet 范围中获得的角色,这些范围存储为数组中的字符串,举个例子,数组看起来像这样:
arr1 = ["football manager","hockey coach", "fb player","fb coach","footballer"];
我还有另一个数组,其中有一小部分标签
arr2 = ["football","fb", "footballer","hockey","rugby"];
我正在尝试将第一个数组的角色与第二个数组中的标签相匹配。
我一直在尝试通过循环获取匹配行的索引来做到这一点:
for(let i in arr1){
arr2.findIndex(s => s.indexOf(arr1[i]) >= 0);
}
但这只适用于“footballer”,因为它是完全匹配,我需要对所有部分匹配进行分类。
使用以下函数查找与 arr1
.
arr2
数组)
详细解释请关注代码注释。
function matchTagIndexes()
{
// TODO replace with your values
arr1 = ["football manager","hockey coach", "fb player","fb coach","footballer"];
// TODO replace with your tags
arr2 = ["football","fb", "footballer","hockey","rugby"];
// for all tags create regex objects
// regex searches for any match that have `tag` surrounded with word (\b) boundaries
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet#boundary-type_assertions
const arr2Regexes = arr2.map(tag => new RegExp(`\b${tag}\b`, 'i'));
// loop arr1 values as val
arr1.map(val =>
// for each arr2 regex match val
arr2Regexes.forEach((regex, i) =>
// if it is matched, log value from arr1 array, matched tag name and tag's index in arr2 array
val.match(regex) && console.log(`"${val}" matches tag "${arr2[i]}" which has index ${i}`)
)
);
}
结果:
time | status | message |
---|---|---|
8:46:35 PM | Notice | Execution started |
8:46:35 PM | Info | "football manager" matches tag "football" which has index 0 |
8:46:35 PM | Info | "hockey coach" matches tag "hockey" which has index 3 |
8:46:35 PM | Info | "fb player" matches tag "fb" which has index 1 |
8:46:35 PM | Info | "fb coach" matches tag "fb" which has index 1 |
8:46:35 PM | Info | "footballer" matches tag "footballer" which has index 2 |
8:46:36 PM | Notice | Execution completed |
参考:
我怀疑某些文本可能有多个标签 (arr1
)。这是获取每个文本的标签(索引)数组的解决方案:
var texts = ['football manager','hockey coach', 'fb player','fb coach','footballer', 'none'];
var tags = ['football','fb', 'footballer','hockey','rugby', 'coach'];
// get all tags for all the texts
var list = [];
for (let tag of tags) {
var mask = RegExp('\b' + tag + '\b', 'i');
for (let text of texts) {
if (text.match(mask))
list.push( {'text': text, 'tag': tag, 'tag_index': tags.indexOf(tag)} );
}
}
console.log(list);
// group tags for the same texts
var text_and_tags = {};
for (let element of list) {
try { text_and_tags[element.text].push(element.tag_index) }
catch(e) { text_and_tags[element.text] = [element.tag_index] }
}
console.log(text_and_tags);
它将为您获取对象 text_and_tags
,如下所示:
{
'football manager': [ 0 ],
'fb player': [ 1 ],
'fb coach': [ 1, 5 ],
'footballer': [ 2 ],
'hockey coach': [ 3, 5 ]
}