Google App Scripts – Regex – Remove all letters, replace all spaces, or new lines with comma not working

huangapple go评论68阅读模式
英文:

Google App Scripts - Regex - Remove all letters, replace all spaces, or new lines with comma not working

问题

INTRO

我有一个Google App Script Web应用程序,其中包含一个表单,其中包含一个文本区域,当提交时,将数据保存在Google表格中。

这个文本区域收集员工ID,并将接收来自不懂技术的用户的输入。

用户可能在输入ID时使用以下格式,包括可能出现的错别字,如额外的空格、逗号或这三种格式的混合。

A) 用空格分隔。

A123456 B001234 TMP00123456

B) 用逗号分隔(有或没有空格)

A123456,B001234, TMP00123456

C) 一行一个。

A123456
B001234
TMP00123456

THE OBJECTIVE

在数据写入Google表格之前,我需要清理上述数据。清理后(期望的结果)数据应该是

123456,001234,00123456

THE PROBLEM

我似乎无法使正则表达式正常工作。我尝试了许多变体。

var agentIds = form.agentIds.replace(/[^\d]+/g, ',').replace(/^,+|,+$/g, '');

简而言之,我正在寻找正则表达式,以...

  1. 用逗号替换所有空格。
  2. 用逗号替换所有换行符。
  3. 删除所有字母。
  4. 如果有两个或更多的逗号相邻,删除多余的逗号。

我不确定这是否是最有效的方法...但这是我能想到的唯一方法。我愿意听取建议。

感谢您的帮助!!! Google App Scripts – Regex – Remove all letters, replace all spaces, or new lines with comma not working

英文:

INTRO

I have a Google App Script Web App which has a form which contains a TextArea, and when submitted saves the data in a Google Sheet.

This text-area collects Employee ID's and will receive input from non-tech savvy users.

The users may use the following formats when entering the ID's into the textarea, including typos such as extra spaces, commas, or a mixture of all 3 formats.

A) Separated by a space.

A123456 B001234 TMP00123456

B) Separated by a comma (with or without a space too)

A123456,B001234, TMP00123456

C) One / line.

A123456
B001234
TMP00123456

THE OBJECTIVE

I need to cleanse the data above before its written to the Google Sheet. The cleansed (expected outcome) data should look like

123456,001234,00123456

THE PROBLEM

I cant seem to get REGEX to work correctly. I have tried many variations.

var agentIds = form.agentIds.replace(/[^\d]+/g, ',').replace(/^,+|,+$/g, '');

In a nutshell, I am looking for regex to...

  1. Replace all spaces with comma.
  2. Replace all line-breaks with commas.
  3. Remove all letters.
  4. IF there are 2 or more commas next to each other, remove the extras.

II am not sure if this is the most efficient way...but the only way I could think of. I am open to suggestions.

Thanks for your help!!! Google App Scripts – Regex – Remove all letters, replace all spaces, or new lines with comma not working

答案1

得分: 1

以下是代码部分的翻译:

const values = ["A123456 B001234 TMP00123456", "A123456,B001234, TMP00123456", "A123456\nB001234\nTMP00123456"];
const res = values.map(e => e.split(/[\s,]+/g).map(f => f.trim().replace(/[A-Z]/ig, "")).join(","));
// 或者 const res = values.map(e => e.replace(/[\s,]+/g, ",").replace(/[A-Z]/ig, ""));
console.log(res);

希望这对您有所帮助。如果您有其他问题,欢迎提出。

英文:

I believe your goal is as follows.

  • You want to retrieve a value of 123456,001234,001234567 from "A123456 B001234 TMP00123456", "A123456,B001234, TMP001234567", "A123456\nB001234\nTMP001234567".

In this case, how about the following sample script? In this sample script, I used split and replace.

Sample script:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const values = [&quot;A123456 B001234 TMP00123456&quot;, &quot;A123456,B001234, TMP00123456&quot;, &quot;A123456\nB001234\nTMP00123456&quot;];
const res = values.map(e =&gt; e.split(/[\s,]+/g).map(f =&gt; f.trim().replace(/[A-Z]/ig, &quot;&quot;)).join(&quot;,&quot;));
// or const res = values.map(e =&gt; e.replace(/[\s,]+/g, &quot;,&quot;).replace(/[A-Z]/ig, &quot;&quot;));
console.log(res);

<!-- end snippet -->

  • When this script is run, [&quot;123456,001234,00123456&quot;,&quot;123456,001234,00123456&quot;,&quot;123456,001234,00123456&quot;] is obtained.

  • In your sample input value and output value, it seems that a value of 001234567 is retrieved from TMP00123456 of A123456 B001234 TMP00123456. I'm worried that you might have miscopied TMP001234567 as TMP00123456. I'm not sure about the detail of this.

  • If you want to use this script for one value, how about the following sample script?

    const sample = &quot;A123456,B001234, TMP00123456&quot;;
    const res = sample.split(/[\s,]+/g).map(f =&gt; f.trim().replace(/[A-Z]/ig, &quot;&quot;)).join(&quot;,&quot;);
    // or const res = sample.replace(/[\s,]+/g, &quot;,&quot;).replace(/[A-Z]/ig, &quot;&quot;);
    console.log(res);
    

References:

huangapple
  • 本文由 发表于 2023年3月1日 15:17:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定