正则表达式替换不替换(谷歌应用脚​​本) [英] regex replace not replacing (google apps script)

查看:133
本文介绍了正则表达式替换不替换(谷歌应用脚​​本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Google应用程序脚本,用于从gmail附件中的CSV文件中提取内容。我需要将csv数据分成几个数组(每行都是它自己的数组)。我已经得到了一切。



我的问题是:CSV中的一个值是City,St格式。所以当我使用string.split(',')分割数组时,我最终得到了一个额外的列。我想解决这个问题的办法是备份并杀死最初字符串中的逗号。这里是我的代码的相关部分:

  var attachments = msgs [i] [j] .getAttachments(); (var k = 0; k< attachments.length; k ++){
var attachmentData = attachments [k] .getDataAsString();
var regex = new RegExp('\,\s(?:[AZ] {2} \)','gi');
attachmentData.replace(regex,'') ;
...

所以我想要做的只是找一个逗号,然后是一个空格,然后是两个字母,然后是一个引号,我想用一个空格替换逗号,我也试过了

  var regex = new RegExp('(\,\s)([AZ] {2} \)','gi'); 
attachmentData.replace(regex,$ 2);

没有运气。这是一个我正在运行的(非常长的)数据字符串的随机样本:


Voice,Incoming,(###) ### - ####,(###)### - #### ,,,,Dallas,TX,12/12 / 2014,06:26 PM,电话,语音信箱,00: 00:27,$ 0.000, - ,,
,传入,(###)### - #### ,, ### ,,,Dallas,TX,12/12 / 2014,06 :26 PM,电话,语音信箱,00:00


任何人都可以看到我没有看到为什么这不是'工作? (或者有更好的方法来做到这一点?)

解决方案


方法返回一个新字符串,其中一些或所有匹配的
替换为一个替换。

str.replace 不会更改字符串,其中as会返回带有replace的新字符串。因此,你可能想写一些像

  var regex = new RegExp('(,\\)([AZ ] {2})','gi'); 
var replacedData = attachmentData.replace(regex,'$ 2');

注意

您可以将第一个捕获组放置为

  var regex = new RegExp(',\\s([AZ] {2})','gi'); 
var replacedData = attachmentData.replace(regex,'$ 1');


I am writing a google apps script that pulls the content from a CSV file in a gmail attachment. I need to split the csv data into several an array (with each row being it's own array). I've got all that down.

My problem is this: One of the values in the CSV is a "City, St" format. So when I split the array using string.split(',') I end up with an extra "column." My idea to fix this was to back up and kill that comma in the initial string. Here's the relevant portion of my code:

    var attachments = msgs[i][j].getAttachments();
    for (var k = 0; k < attachments.length; k++) {
      var attachmentData = attachments[k].getDataAsString();
      var regex = new RegExp('\,\s(?:[A-Z]{2}\")', 'gi');
      attachmentData.replace(regex,' ');
...

So what I'm trying to do is just find a comma, followed by a space, followed by exactly two letters, then a quotation mark. I want to just replace the comma with a space. I've also tried

      var regex = new RegExp('(\,\s)([A-Z]{2}\")', 'gi');
      attachmentData.replace(regex,$2);

with no luck. Here's a random sample of the (very long) data string I'm running this on:

Voice,Incoming,(###) ###-####,(###) ###-####,,,,"Dallas, TX",12/12/2014,06:26 PM,Phone Call,Voicemail,00:00:27,$0.000,-,, ,Incoming,(###) ###-####,,###,,,"Dallas, TX",12/12/2014,06:26 PM,Phone Call,Voicemail,00:00

Can anyone see what I'm not seeing as to why this isn't working? (Or have any ideas of a better way to do this?)

解决方案

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.

The str.replace does not change the string, where as returns a new string with the replace. Hence you may want to write something like

var regex = new RegExp('(,\\s)([A-Z]{2}")', 'gi');
var replacedData = attachmentData.replace(regex,'$2');

Note

You can drop the first capture group as

var regex = new RegExp(',\\s([A-Z]{2}")', 'gi');
var replacedData = attachmentData.replace(regex,'$1');

这篇关于正则表达式替换不替换(谷歌应用脚​​本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆