简体   繁体   English

如果在特定字符串旁边,则搜索两个字符之间的字符串

[英]Search for string between two characters if next to a specific string

I am in need of some modification to my function to allow for a search of two strings on one line of a value. 我需要对函数进行一些修改,以允许在值的一行上搜索两个字符串。 I am trying to work through this on my own but I need some help. 我正在尝试自己完成这项工作,但我需要一些帮助。 Here is an example of a cell value being looked at. 这是查看单元格值的示例。 Assume there are no leading or trailing newlines. 假设没有前导或尾随的换行符。 Also, all the cells have the same format. 而且,所有单元格都具有相同的格式。 same number of lines, same structure of membertype: last, first etc. 相同的行数,相同的成员类型结构:最后一个,第一个,等等。

在此处输入图片说明

Say I want to see if this cell contains a team lead with the name of last2 or a Manager with the name first4 . 说我想看看这个单元包含一个team lead与名last2Manager的名称first4 Both the type of employee and name would be user inputted. 雇员的类型和姓名都将由用户输入。

I tried using the following that I created with the help of this . 我尝试使用在this的帮助下创建的以下内容。

indexOf(':(.*):')

It returns the position of the content between and including the colons. 它返回冒号之间(包括冒号)的内容的位置。 Then I tried the following: 然后我尝试了以下方法:

flatUniqArr[0].search('Supervisor:')

This is where I'm stuck. 这就是我卡住的地方。 It returns the index to the last digit of the first line. 它将索引返回到第一行的最后一位。

My thought was to do a search of the user inputted name between the colons if they follow the user inputted member type. 我的想法是如果冒号遵循用户输入的成员类型,则在冒号之间搜索用户输入的名称。 How can I accomplish this? 我该怎么做?

Clarifications: The end goal is to verify that the name and member type are on the same line and excluded from an array I am building for .setHiddenValues() . 说明:最终目标是验证名称和成员类型是否在同一行上,并从我为.setHiddenValues()构建的数组中排除。 So if they are on the same line exclude from list. 因此,如果它们在同一行上,则从列表中排除。

Here is the function I will be adding it to: 这是我将添加到的功能:

var flatUniqArr = colValueArr.map(function(e){return e[0].toString();})
.filter(function(e,i,a){
  return (a.indexOf(e) == i && !(visibleValueArr.some(function(f){
    return e.search(new RegExp(f,'i')) + 1;
  })));
});
return flatUniqArr;

Where flatUniqArr is the list of hidden values. 其中flatUniqArr是隐藏值的列表。 colValueArr is the array of values from a column. colValueArr是列中值的数组。 visibleValueArr is the name which is user inputted and memberType will be the member type. visibleValueArr是用户输入的名称, memberType将是成员类型。

Attempts using Liora's solution: (Updated... Works now) 尝试使用Liora的解决方案:(已更新...现在可以使用)

var flatUniqArr = []
var lines = []
Logger.log(visibleValueArr)
Logger.log(memberType)
for (var i = 0; i < colValueArr.length; i++){
  lines = colValueArr[i].toString().split('\n');
  var found = false;
  for(var j = 0; j < lines.length; j++){
    var data = lines[j].toLowerCase().split(':')
    if(data[0] == memberType.toString().toLowerCase() && data[1].indexOf(visibleValueArr.toString().toLowerCase()) != -1){
      found = true;
    }
  }
  Logger.log(found)
  if(found == false){flatUniqArr.push(colValueArr[i])} 
}

return flatUniqArr;

It works now. 现在可以使用了。 It seems like a lot of code though. 虽然看起来好像很多代码。 I'd be open to alternative solutions if they are faster and/or less lines of code. 如果它们是更快和/或更少的代码行,我将接受替代解决方案。

Updated: Added .toString().toLowerCase() as the user may input lowercase values. 更新:添加了.toString().toLowerCase()因为用户可以输入小写值。

How about just building the regex using the user input? 仅使用用户输入来构建正则表达式怎么样?

function search(line, employeeType, employeeName) {
    var regexp = '/' + employeeType + ': ' + employeeName + '/'
    return line.search(regexp)
}

Or better yet, if it always occurs at the beginning of the string, just use startsWith() 更好的是,如果它总是出现在字符串的开头,则只需使用startsWith()

I assume all the line have this format. 我假设所有行都具有这种格式。

If you split each line with the separator ":" 如果用分隔符“:”分隔每一行

var array = value.split(":")

Then you'd have 那你有

array[0] //the current role
array[1] //the list of name
array[2] //the email

And you can check each names then 然后您可以检查每个名称

if(array[0] == "Team Lead" && array[1].indexOf("last2") != -1)

An example with a linesplit: 一个带有线分割的示例:

var lines = value.toString().split("\n");
var found = false;
for(var i = 0; i < lines.length ; i++){
    var data = value.split(":")
    if(data[0] == "Team Lead" && data[1].indexOf("last2") != -1){
         found = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM