Google Apps 脚本中的正则表达式实际问题。表单没有按应有的方式读取正则表达式

Regex in Google Apps Script practical issue. Forms doesn't read regex as it should

我希望这只是我做错了什么。

我一直在使用一个简单的脚本从电子表格中创建一个表单。该脚本似乎工作正常。输出表格将从第三方获得一些输入,因此我可以在我的咨询中分析它们 activity。

创建表单没什么大不了的,结构很好。但是,在让表单创建者脚本运行之后,我开始进行验证,这就是我遇到的问题。

对于文本验证,我需要使用特定的正则表达式。我的客户需要给我的许多输入将是地名 and/or 人名,因此,我应该只允许他们使用 A-Z、单个空格、撇号和破折号。

我生成的正则表达式是:

//Regex allowing a **single name** with the first letter capitalized and the occasional use of "apostrophes" or "dashes". 
const reg1stName = /^[A-Z]([a-z\'\-])+/
//Should allow (a single name/surname) like Paul, D'urso, Mac'arthur, Saint-Germaine ecc.
//Regex allowing **composite names and places names** with the first letter capitalized and the occasional use of "apostrophes" or "dashes". It must avoid double spaces, however.
const regNamesPlaces = /^[^\s]([A-Z]|[a-z]|\b[\'\- ])+[^\s]$/  
//This should allow (names/surnames/places' names) like Giulius Ceasar, Joanne D'arc, Cosimo de'Medici, Cosimo de Medici,  Jean-jacques Rousseau, Firenze, Friuli Venezia-giulia, L'aquila ecc.

进一步在脚本中,这些正则表达式被称为表单文本项的验证模式,根据每个案例。

//Validation for single names
 var val1stName = FormApp.createTextValidation()
  .setHelpText("Only the person First Name Here! Use only (A-Z), a single apostrophe (') or a single dash (-).")
  .requireTextMatchesPattern(reg1stName)
  .build();
//Validation for composite names and places names   
 var valNamesPlaces = FormApp.createTextValidation()
  .setHelpText(("Careful with double spaces, ok? Use only (A-Z), a single apostrophe (') or a single dash (-)."))
  .requireTextMatchesPattern(regNamesPlaces)
  .build();

此外,我有一个 "for" 循环,可以根据电子表格字段创建表单。到目前为止,一切正常。

 for(var i=0;i<numberRows;i++){
  var questionType = data[i][0]; 
  if (questionType==''){
     continue;
  }
  else if(questionType=='TEXTNamesPlaces'){
   form.addTextItem()
     .setTitle(data[i][1]) 
     .setHelpText(data[i][2])
     .setValidation(valNamesPlaces)
     .setRequired(false);   
  }
  else if(questionType=='TEXT1stName'){
   form.addTextItem()
     .setTitle(data[i][1]) 
     .setHelpText(data[i][2])
     .setValidation(val1stName)
     .setRequired(false);
  }

问题是当我 运行 脚本并测试生成的表单时。 两种验证类型都可以正常导入(可以在表单的编辑模式中看到),但是在预览模式下测试时出现错误,就好像正则表达式不匹配一样(对不起,错误消息是葡萄牙语,我忘了像我对上面的代码所做的那样翻译它们):

A screenshot of the form in edit mode

A screeshot of the form in preview mode

但是,如果我手动删除此正则表达式中的栏“//”,它就会开始工作!

A screenshot of the form in edit mode, Regex without bars

A screenshot of the form in preview mode, Regex without bars

我做错了什么?我不是专业的开发人员,但据我所知,编写没有条形码的 Regex 是没有意义的。

如果这是读取正则表达式的某种 Gforms 模式,我仍然需要创建此表单的 Apps 脚本读取所有这些内容。如果我什至尝试在没有栏的情况下传递正则表达式,脚本将无法读取它。

const reg1stName = ^[A-Z]([a-z\'])+
const regNamesPlaces = ^[^\s]([A-Z]|[a-z]|\b[\'\- ])+[^\s]$
//Can't even be saved. Returns: SyntaxError: Unexpected token '^' (line 29, file "Code.gs")

手动通过所有验证不是一种选择。有人可以帮助我吗?

非常感谢

这个

/^[A-Z]([a-z\'\-])+/

将无法工作,因为解析器正在尝试将您的 / 作为字符串文字进行匹配。

这个

^[A-Z]([a-z\'\-])+

也不行,因为如果名称有连字符,您将只匹配连字符。例如,这将匹配 'Some-Name' 中的 'Some-'。另外,也许您还希望 'Saint John' 这样的名称也通过?

我推荐以下:)

^[A-Z][a-z]*[-\.' ]?[A-Z]?[a-z]*

^ 锚定到字符串的开头

[A-Z] 正好匹配 1 个大写字母

[a-z]* 匹配零个或多个小写字母(这使您能够匹配像 D'Urso 这样的名字)

[-\.' ]? 匹配零个或 1 个 -(连字符)、. (句点)、' (撇号) 或单个 space (. (句点) 需要用反斜杠转义,因为 . 对于正则表达式是特殊的)

[A-Z]? 匹配零个或 1 个大写字母(如果名称中有第二个大写字母,例如 D'Urso、St John、Saint-Germaine)