Sam Jones
Sam Jones

Reputation: 4618

regex replace in javascript/jquery

I've never really used regex so this is probably a basic question, but I need to reformat a string in javascript/jquery and I think regex is the direction to go.

How can I convert this string:

\"1\",\"2\",\"\\",\"\4\"

into:

"1","2","","4"

These are both strings, so really they'd be contained in "" but I thought that may confuse things even more.

I've tried the following but it doesn't work:

var value = '\"1\",\"2\",\"\\",\"\4\"'.replace(/\"/, '"').replace(/"\//, '"');

Upvotes: 0

Views: 116

Answers (2)

mishik
mishik

Reputation: 10003

Try:

var value = your_string.replace(/\\/g, "");

to remove all the "\"

Upvotes: 3

Cjxcz Odjcayrwl
Cjxcz Odjcayrwl

Reputation: 22847

It's a lot of escaping... Your string is:

var str = '\\"1\\",\\"2\\",\\"\\\\",\\"\\4\\"'

console.log(str.replace(/\\/g, '')) // "1","2","","4"

However, if you want only to replace \" with " use:

console.log(str.replace(/\\"/g, '"')) // "1","2","\","\4"

Upvotes: 1

Related Questions