Find comma in quotes with regex and replace with HTML equiv

前端 未结 4 1486
终归单人心
终归单人心 2020-12-21 16:38

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&         


        
4条回答
  •  -上瘾入骨i
    2020-12-21 17:18

    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:

    • CSV style (with two double quotes) - /"([^"]|"")*"/g
    • String literal style - /"([^"\\]|\\.)*"/g

提交回复
热议问题