-1

I am trying to convert string that has following values

"A\"s\"sets"

my goal is to remove from string \ values no matter how many of them appear in string.

 "A"s"sets"

I tried using new RegExp but I do not manage to perform that operation. I even managed to create regex that will pick up everything except \ sign

[a-zA-Z0-9'"*]

I also tried calling on

regex.exec(string)

but I am getting an array instead of cleared string. Anyone have any idea how to do this ? Thank you

zawarudo
  • 1,907
  • 2
  • 10
  • 20
  • Those slashes \ that you see aren't actually slashes, they escape the special meaning characters like `"` needs to be escaped in a string. – Pushpesh Kumar Rajwanshi Jan 30 '19 at 16:21
  • It does not provide proper result unless your string has \ one next to another, here is a fiddle of an example with randomly inputed \ in string http://jsfiddle.net/hwe869ro/ – zawarudo Jan 30 '19 at 16:27

1 Answers1

0

You can use replace.

let str = `"A\"s\\"sets"`

let op = str.replace(/\\+/g, '')

console.log(op)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • 1
    That doesn't work. It only appears to work because you constructed the value of `str` incorrectly so the slashes are treated as escape sequences and are not part of the data. Your regular expression removes `+` characters! Since there aren't any, `op === str` – Quentin Jan 30 '19 at 16:26
  • @Quentin updated now it will – Code Maniac Jan 30 '19 at 16:28
  • @CodeManiac: If you really want to have slash in your string, you need to escape it and write double slashes in the string. Try running this `console.log(str)` You will see there are no slashes printed in the string. – Pushpesh Kumar Rajwanshi Jan 30 '19 at 16:35
  • @PushpeshKumarRajwanshi i think you didn't noticed the updated one ? did you ? – Code Maniac Jan 30 '19 at 16:37
  • That one works great even if I get mangled string that escaped 10 double quotes like "A\"s\"\"\"\"\"s\"e\"ts" it will extract all slashes, thank you so much CM ! The problem is that I am getting string from velocity and this is what I am getting and what I need to intercept in order to avoid any value issues when it reaches JS, otherwise it would not be the problem. Thank you again CM ! – zawarudo Jan 30 '19 at 16:42