英文:
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, '');
简而言之,我正在寻找正则表达式,以...
- 用逗号替换所有空格。
- 用逗号替换所有换行符。
- 删除所有字母。
- 如果有两个或更多的逗号相邻,删除多余的逗号。
我不确定这是否是最有效的方法...但这是我能想到的唯一方法。我愿意听取建议。
感谢您的帮助!!!
英文:
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...
- Replace all spaces with comma.
- Replace all line-breaks with commas.
- Remove all letters.
- 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!!!
答案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 = ["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(","));
// or const res = values.map(e => e.replace(/[\s,]+/g, ",").replace(/[A-Z]/ig, ""));
console.log(res);
<!-- end snippet -->
-
When this script is run,
["123456,001234,00123456","123456,001234,00123456","123456,001234,00123456"]
is obtained. -
In your sample input value and output value, it seems that a value of
001234567
is retrieved fromTMP00123456
ofA123456 B001234 TMP00123456
. I'm worried that you might have miscopiedTMP001234567
asTMP00123456
. 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 = "A123456,B001234, TMP00123456"; const res = sample.split(/[\s,]+/g).map(f => f.trim().replace(/[A-Z]/ig, "")).join(","); // or const res = sample.replace(/[\s,]+/g, ",").replace(/[A-Z]/ig, ""); console.log(res);
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论