I\'m looking in a string such as:
\"Hello, Tim\"
Land of the free, and home of the brave
And I need it to become:
\"Hello&
result = subject.replace(/("[^"]+?),([^"]*?")/img, "$1,$2");
This will work properly with your example, the only catch is it will not work if you have multiple ,
inside of the "
. If you need it to work with multiple ,
inside of the "
then take a look at this for a more complete way to parse CSV data with javascript.
With the above string as variable html
you can use following code:
var m = html.match(/"[\s\S]*"/);
html = html.replace(m[0], m[0].replace(/,/g, ','));
"Hello, Tim"
Land of the free, and home of the brave
var str = '"Hello, Tim"\n\
\n\
Land of the free, and home of the brave';
str
.split('"')
.map(function(v,i){ return i%2===0 ? v : v.replace(',',','); })
.join('"');
Check MDC for an implementation of map()
for non-supporting browsers.
It is probably easier with a callback function to replace:
s = s.replace(/"[^"]*"/g, function(g0){return g0.replace(/,/g,',');});
At the first step we find all quotes, and replace just the commas in them.
You can even allow escaping quotes:
/"([^"]|"")*"/g
/"([^"\\]|\\.)*"/g