0

I have this string

var behavior = "["Automático", "Manual", "Automático"]"

And i would like to get the same behavior variable but with something like that:

var behavior = "[Automático, Manual, Automático]" 

using regex or other solution if it works.! Thaks.

  • Welcome to Stackoverflow. Please visit the [help], take the [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output. – mplungjan May 15 '19 at 12:17
  • 1
    You mean `var behavior = ["Automáticot", "Manual", "Automático"]` right? – mplungjan May 15 '19 at 12:18
  • Possible duplicate of [Fastest method to replace all instances of a character in a string](https://stackoverflow.com/questions/2116558/fastest-method-to-replace-all-instances-of-a-character-in-a-string) – Kld May 15 '19 at 12:20
  • Do you want a *string* or an actual array in the end? And if it's a string, do you want `Automático&quot`? – VLAZ May 15 '19 at 12:26
  • I want the same string but with the format "[Automático, Manual, Automático]", keeping on mind that the variable I'm working on is a string, and not an array and I want the same variable but with the aforementioned format. Thanks – Adrian Rama Aguilar May 15 '19 at 18:02

2 Answers2

1

You are looking for JSON.parse

var behavior = "["Automático", "Manual", "Automático"]"
console.log(
  JSON.parse(behavior.replace(/"/g,'"'))
)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

The solutions Thaks to mplungjan.

var behavior = "["Automático", "Manual", "Automático"]";
behavior = behavior.replace(/"/g,'"');
console.log(behavior)