Find comma in quotes with regex and replace with HTML equiv

前端 未结 4 1497
终归单人心
终归单人心 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条回答
  •  情书的邮戳
    2020-12-21 17:12

    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.

提交回复
热议问题