0

我需要重新组织一个古老的 cvs 数据库,以便可以使用正确的特殊字段名称将其导入电子商务软件。

这是我匹配的一些代码。[id,类别,制造商,型号]

"1","3","Samsung","Galaxy S III"
"2","3","HTC","One X"

我的正则表达式

".*?".*?".*?".*?(".*?")

我基本上只需要用稍微复杂的对象文字替换制造商,比如

{manufacturers: 'Samsung'}

但我似乎无法弄清楚如何选择性地替换第三场比赛。我知道我可以拆分字符串并提取我需要的数据,但我不知道如何替换匹配项,以便输出变量如下所示:

"1","3",{manufacturers: 'Samsung'},"Galaxy S III"
"2","3",{manufacturers: 'HTC'},"One X"

谢谢你帮助我!

4

1 回答 1

2

匹配这个:

"(.*?)","(.*?)","(.*?)","(.*?)"

替换为:

"$1","$2",{manufacturers: '$3'},"$4"

例子:

var s='"1","3","Samsung","Galaxy S III"';
var p='"(.*?)","(.*?)","(.*?)","(.*?)"';
var r='"$1","$2",{manufacturers: \'$3\'},"$4"';
console.log(s.replace(new RegExp(p,"g"),r));

输出:

"1","3",{manufacturers: 'Samsung'},"Galaxy S III"
于 2012-04-18T11:59:55.433 回答