我需要重新组织一个古老的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"
感谢您的帮助!
答案 0 :(得分: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"