用字符串中的引号替换转义引号

时间:2021-12-19 00:08:28

So I'm having an issue replacing \" in a string.

所以我在一个字符串中替换\“有问题。

My Objective: Given a string, if there's an escaped quote in the string, replace it with just a quote

我的目标:给定一个字符串,如果字符串中有一个转义引号,则只用引号替换它

So for example:

例如:

"hello\"74"  would be "hello"74"
simp"\"sons would be simp"sons
jump98" would be jump98"

I'm currently trying this: but obviously that doesn't work and messes everything up, any assistance would be awesome

我现在正在尝试这个:但显然这不起作用并且搞砸了一切,任何帮助都会很棒

str.replace "\\"", "\""

3 个解决方案

#1


2  

I guess you are being mistaken by how \ works. You can never define a string as

我猜你被错误的方式弄错了。您永远不能将字符串定义为

a = "hello"74"

Also escape character is used only while defining the variable its not part of the value. Eg:

转义字符仅在定义变量时使用,而不是值的一部分。例如:

a = "hello\"74" 
# => "hello\"74"
puts a
# hello"74

However in-case my above assumption is incorrect following example should help you:

但是,如果我的上述假设不正确,以下示例可以帮助您:

a = 'hello\"74'
# => "hello\\\"74"

puts a
# hello\"74

a.gsub!("\\","")
# => "hello\"74"

puts a
# hello"74

EDIT

编辑

The above gsub will replace all instances of \ however OP needs only to replace '" with ". Following should do the trick:

上面的gsub将替换\的所有实例,但OP只需要替换'“with”。以下应该做的诀窍:

a.gsub!("\\\"","\"")
# => "hello\"74"
puts a
# hello"74

#2


1  

You can use gsub:

你可以使用gsub:

word = 'simp"\"sons';
print word.gsub(/\\"/, '"');
//=> simp""sons

#3


0  

I'm currently trying str.replace "\\"", "\"" but obviously that doesn't work and messes everything up, any assistance would be awesome

我现在正在尝试str.replace“\\”“,”\“”但显然这不起作用并且搞砸了一切,任何帮助都会很棒

str.replace "\\"", "\"" doesn't work for two reasons:

str.replace“\\”“,”\“”有两个原因:

  1. It's the wrong method. String#replace replaces the entire string, you are looking for String#gsub.
  2. 这是错误的方法。 String#replace替换整个字符串,您正在寻找String#gsub。
  3. "\\"" is incorrect: " starts the string, \\ is a backslash (correctly escaped) and " ends the string. The last " starts a new string.
  4. “\\”“不正确:”启动字符串,\\是反斜杠(正确转义)和“结束字符串。最后一个”启动一个新字符串。

You have to either escape the double quote:

你必须要么逃避双引号:

puts "\\\"" #=> \"

Or use single quotes:

或者使用单引号:

puts '\\"' #=> \"

Example:

例:

content = <<-EOF
"hello\"74"
simp"\"sons
jump98"
EOF

puts content.gsub('\\"', '"')

Output:

输出:

"hello"74"
simp""sons
jump98"

#1


2  

I guess you are being mistaken by how \ works. You can never define a string as

我猜你被错误的方式弄错了。您永远不能将字符串定义为

a = "hello"74"

Also escape character is used only while defining the variable its not part of the value. Eg:

转义字符仅在定义变量时使用,而不是值的一部分。例如:

a = "hello\"74" 
# => "hello\"74"
puts a
# hello"74

However in-case my above assumption is incorrect following example should help you:

但是,如果我的上述假设不正确,以下示例可以帮助您:

a = 'hello\"74'
# => "hello\\\"74"

puts a
# hello\"74

a.gsub!("\\","")
# => "hello\"74"

puts a
# hello"74

EDIT

编辑

The above gsub will replace all instances of \ however OP needs only to replace '" with ". Following should do the trick:

上面的gsub将替换\的所有实例,但OP只需要替换'“with”。以下应该做的诀窍:

a.gsub!("\\\"","\"")
# => "hello\"74"
puts a
# hello"74

#2


1  

You can use gsub:

你可以使用gsub:

word = 'simp"\"sons';
print word.gsub(/\\"/, '"');
//=> simp""sons

#3


0  

I'm currently trying str.replace "\\"", "\"" but obviously that doesn't work and messes everything up, any assistance would be awesome

我现在正在尝试str.replace“\\”“,”\“”但显然这不起作用并且搞砸了一切,任何帮助都会很棒

str.replace "\\"", "\"" doesn't work for two reasons:

str.replace“\\”“,”\“”有两个原因:

  1. It's the wrong method. String#replace replaces the entire string, you are looking for String#gsub.
  2. 这是错误的方法。 String#replace替换整个字符串,您正在寻找String#gsub。
  3. "\\"" is incorrect: " starts the string, \\ is a backslash (correctly escaped) and " ends the string. The last " starts a new string.
  4. “\\”“不正确:”启动字符串,\\是反斜杠(正确转义)和“结束字符串。最后一个”启动一个新字符串。

You have to either escape the double quote:

你必须要么逃避双引号:

puts "\\\"" #=> \"

Or use single quotes:

或者使用单引号:

puts '\\"' #=> \"

Example:

例:

content = <<-EOF
"hello\"74"
simp"\"sons
jump98"
EOF

puts content.gsub('\\"', '"')

Output:

输出:

"hello"74"
simp""sons
jump98"