用regex在引号中找到逗号,并用HTML equiv替换 [英] Find comma in quotes with regex and replace with HTML equiv

查看:81
本文介绍了用regex在引号中找到逗号,并用HTML equiv替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查找如下字符串:

I'm looking in a string such as:

"Hello, Tim"

Land of the free, and home of the brave

我需要它成为:

"Hello, Tim"

Land of the free, and home of the brave

我觉得这很简单,但对于我的生活,我无法理解。我通过AJAX导入一个包含1000个条目的损坏的CSV,所以我只需要将逗号转换为双引号INSIDE。

This I thought was simple, but for the life of me I can't figure it out. Im importing a corrupt CSV with 1000s of entries via AJAX, so I just need to convert commas INSIDE of double quotes.

我将如何使用JavaScript执行此操作?我无法弄清楚。我试过了

How would I go about do this with JavaScript? I can't figure it out. I tried

推荐答案

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('"');

检查MDC ,了解非支持浏览器的 map()的实现。

Check MDC for an implementation of map() for non-supporting browsers.

这篇关于用regex在引号中找到逗号,并用HTML equiv替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆