Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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
Ruby on rails “我怎样才能逃脱?”;或';当它是ruby或rails中的变量时?_Ruby On Rails_Ruby_String - Fatal编程技术网

Ruby on rails “我怎样才能逃脱?”;或';当它是ruby或rails中的变量时?

Ruby on rails “我怎样才能逃脱?”;或';当它是ruby或rails中的变量时?,ruby-on-rails,ruby,string,Ruby On Rails,Ruby,String,我正在后端构建一个js文件以备将来使用,需要将宏转换为js代码。我的gsub在下面的方法中工作得很好,直到我有了一个带有双引号的url。如果我有一个变量quote_type=“””,如何将其作为转义引号插入?换句话说 “'”变为“\'”而“变为“\” 这是我现有的方法,我刚刚添加了quote\u type变量来尝试这个方法 def substitute_timestamp_macro!(string) quote_type = string[0] string.gsub('[timest

我正在后端构建一个js文件以备将来使用,需要将宏转换为js代码。我的
gsub
在下面的方法中工作得很好,直到我有了一个带有双引号的url。如果我有一个变量
quote_type=“””
,如何将其作为转义引号插入?换句话说

“'”
变为
“\'”
变为
“\”

这是我现有的方法,我刚刚添加了
quote\u type
变量来尝试这个方法

def substitute_timestamp_macro!(string)
  quote_type = string[0]
  string.gsub('[timestamp]', '\' + new Date().getTime() + \'')
end
编辑:示例字符串为
“https://doesntmatter.com/stuff/123;时间=[时间戳]?“

编辑2:以下是预期结果:
”https://doesntmatter.com/stuff/123;time=“+new Date().getTime()+”?“

以下是实际结果
”https://doesntmatter.com/stuff/123;时间='+新日期().getTime()+'?“

注意,在我的
gsub
中,我硬编码了一个转义单引号。现在我想使用变量来匹配输入引号,因此如果它是单引号,请使用单引号,如果是双引号,请使用双引号来包装字符串:

def substitute_timestamp_macro!(string)
  %("#{string}").gsub("[timestamp]", %(" + new Date().getTime() + "))
end
请注意,这将呈现带有转义双引号的字符串:

substitute_timestamp_macro!("https://doesntmatter.com/stuff/123;time=[timestamp]")
=> "\"https://doesntmatter.com/stuff/123;time=\" + new Date().getTime() + \"?\""
将其渲染到缓冲区时,将正确解释:

puts substitute_timestamp_macro!("https://doesntmatter.com/stuff/123;time=[timestamp]")
=> "https://doesntmatter.com/stuff/123;time=" + new Date().getTime() + "?"

值的周围引号字符决定此函数的行为,这应该适合账单:

def substitute_timestamp_macro!(string)
  quote_type = string[0]
  string.gsub('[timestamp]', "\\#{quote_type} + new Date().getTime() + \\#{quote_type}")
end

你能显示预期结果和实际结果吗?这里的“带双引号的URL”是什么意思?我看到了你添加的URL,你的意思是该URL中的双引号在值中并且有意义吗?你希望翻译的最终结果是什么,用引号来表示。你能展示一个前后示例吗?@maxpleaner请查看编辑2@MichaelGaskill我已经在编辑中澄清了。我问题的第一部分是关于你所指的双引号。示例字符串周围的双引号是否就是您所指的双引号?这些是来自你收到的URL值,还是你在问其他问题?这很有效!我将不得不做一些调整,但我可以看到这对我的工作。谢谢没问题,祝你好运!谢谢这个。。。不幸的是,在我的上下文中,
quote\u类型
需要转义。现在我想起来了,也许文字上的逃避也能奏效<代码>“\\\{quote\u type}”我明白了。在这方面,我将更新答案,使其正确。文字转义是最简单的解决方案,所以我使用了它。谢谢