简体   繁体   English

使用正则表达式在 Google Sheets 应用脚本中查找替换不起作用

[英]Find Replace In Google Sheets App Scripts With Regex Not Working

I have read many posts about this on SO.我在 SO 上阅读了很多关于此的帖子。 I am having trouble using Regex in google scripts for sheets.我在 google 脚本中使用 Regex 时遇到问题。

I have a list of domains.我有一个域列表。

Goal: I want to remove all the path information from every domain in the list (everything after the .*/ )目标:我想从列表中的每个域中删除所有路径信息( .*/之后的所有内容)

For example, I want to turn this:例如,我想转这个:

example.com/path-after-domain

example2.com/another-path-afterDomain

into this:进入这个:

example1.com

example2.com

Here is what I am trying to do.这是我想要做的。

Looping and replace function循环替换function

function replaceInSheet(values, to_replace, replace_with) {
  
  for(var row in values){
 
  var replaced_values = values[row].map(function(original_value) {
    return original_value.toString().replace(to_replace,replace_with)
  })
}
}

Using the function by passing Regex通过传递正则表达式使用 function

replaceInSheet(values, '((\/\w+)*\/)(.*)', "")

I also tested using createtextfinder .我还使用createtextfinder进行了测试。 But I get an error when trying to use regex但是尝试使用正则表达式时出现错误

const check = range.createTextFinder("((\/\w+)*\/)(.*)").useRegularExpression(true)

check.replaceWith("")

Note - I tested the regex with https://regex101.com/ and it successfully gets all path information注意 - 我用https://regex101.com/测试了正则表达式,它成功获取了所有路径信息

I believe your goal is as follows.我相信你的目标如下。

  • You want to achieve the following conversion using Google Apps Script.您想使用 Google Apps 脚本实现以下转换。

    • From

       example.com/path-after-domain, example2.com/another-path-afterDomain
    • To

       example1.com, example2.com

In this case, how about using split as follows?在这种情况下,如何使用split如下?

Sample script:示例脚本:

 const values = ["example.com/path-after-domain", "example2.com/another-path-afterDomain"]; const res = values.map(e => e.split("/")[0]); console.log(res) // [ 'example.com', 'example2.com' ]

Note:笔记:

  • If you want to use this as a custom function, how about the following sample script?如果您想将此用作自定义 function,以下示例脚本怎么样? In this case, please put a custom function like =SAMPLE(A1:A10) to a cell.在这种情况下,请将自定义 function 像=SAMPLE(A1:A10)放到一个单元格中。

     const SAMPLE = values => values.map(r => r.map(c => c.split("/")[0]));
  • If you want to use TextFinder using your regex of ((\/\w+)*\/)(.*) , how about the following sample script?如果您想使用((\/\w+)*\/)(.*)的正则表达式来使用 TextFinder,那么下面的示例脚本怎么样? In this case, please escape \ .在这种情况下,请转义\ I thought that \\/.* might be able to be also used.我认为\\/.*可能也可以使用。

     const sheet = SpreadsheetApp.getActiveSheet(); sheet.getRange("A1:A" + sheet.getLastRow()).createTextFinder("((\\/\\w+)*\\/)(.*)").useRegularExpression(true).replaceAllWith("");
    • In this case, the column "A" is used.在这种情况下,使用列“A”。
  • If the built-in function is used, how about the following sample formula?如果使用内置的 function,下面的示例公式怎么样?

     =ARRAYFORMULA(IFERROR(INDEX(SPLIT(A1:A,"/"),, 1))) =ARRAYFORMULA(REGEXREPLACE(A1:A,"/.*",""))

Reference:参考:

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

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