Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 模式在regex101上工作,但不使用Google脚本_Javascript_Regex_Google Apps Script - Fatal编程技术网

Javascript 模式在regex101上工作,但不使用Google脚本

Javascript 模式在regex101上工作,但不使用Google脚本,javascript,regex,google-apps-script,Javascript,Regex,Google Apps Script,我正在尝试匹配Google文档中的一些段落,但是我想要用于它的模式在Google脚本中运行时与字符串不匹配。但是,它可以正常工作,所以我想我遗漏了一些东西。你知道吗 这是我拥有的一个样本: function test() { var str = "brown fox → jumps over the lazy dog"; var definitionRe = new RegExp('([\w\s]+)\s+[\u2192]\s+(.+)', 'g'); var definitionM

我正在尝试匹配Google文档中的一些段落,但是我想要用于它的模式在Google脚本中运行时与字符串不匹配。但是,它可以正常工作,所以我想我遗漏了一些东西。你知道吗

这是我拥有的一个样本:

function test() {
  var str = "brown fox → jumps over the lazy dog";
  var definitionRe = new RegExp('([\w\s]+)\s+[\u2192]\s+(.+)', 'g');
  var definitionMatch = definitionRe.exec(str); // null

  var dummy = "asdf"; // makes the debugger happy to break here
}

当使用字符串正则表达式(如
newregexp(…)
)时,需要转义
\
,然后执行以下操作:

var definitionRe = new RegExp('([\w\s]+)\s+[\u2192]\s+(.+)', 'g');
将成为如下所示的转义版本:

var definitionRe = new RegExp('([\\w\\s]+)\\s+[\\u2192]\\s+(.+)', 'g');
否则,您可以执行非字符串版本,但随后无法再将值连接到字符串(如果您愿意):


我认为您需要摆脱转义模式,因此
\w\s
变成
\\w\\s
等等…@GetOffMyLawn现在是2slashes@CodeManiac谢谢,我更新了it@GetOffMyLawn哇,你说得对!现在开始工作了!谢谢;-)我不认为这是重复的,所以它被错误地关闭了。
var definitionRe = /([\w\s]+)\s+[\u2192]\s+(.+)/g;