问题
我正在进行数据库迁移,遇到以下senario,在插入的单引号内有单引号,经过几次尝试修复后,我得出结论,我需要使用正则表达式,并且到达了到可以替换文本中心单引号的地步,当它出现在开头时,我现在遇到了问题。我的正则表达式具有前两个单引号,但我的目标是后两个。
SQL插入:
(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
- Lorem ipsum dolor '' sit amet - ''''
Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
Integer sit amet lacus ornare massa feugiat tempus.
', null, null);
我的Regex(js):
((["'])(?:(?=(\'\'\?))\2)*?)\1(?!,)
我在进行正则表达式后找到并替换的目标是将其作为结果
(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '\'\'field3', '
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
- Lorem ipsum dolor \'\' sit amet - \'\'\'\'
Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
Integer sit amet lacus ornare massa feugiat tempus.
', null, null);
我正在使用此网站进行测试:https://regex101.com/(已选择javascript)
答案 0 :(得分:1)
一种选择是匹配一个'
或"
分隔符,然后延迟重复直到再次匹配该分隔符后跟一个逗号(或{{1 }}(如果项目是列表中的最后一个)。然后,使用替换函数将定界符内的所有内容替换为)
的转义符:
'
https://regex101.com/r/RRmq8g/1
如果要在定界符为const input = `(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
- Lorem ipsum dolor '' sit amet - ''''
Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
Integer sit amet lacus ornare massa feugiat tempus.
', null, null);`;
const output = input.replace(
/(["'])([\s\S]*?)\1(?=[,)])/g,
(_, delim, content) => delim + content.replace(/'/g, "\\'") + delim
);
console.log(output);
时转义"
,请从"
构造替换正则表达式:
delim
const output = input.replace(
/(["'])([\s\S]*?)\1(?=[,)])/g,
(_, delim, content) => {
const pattern = new RegExp(delim, 'g')
return delim + content.replace(pattern, '\\' + delim) + delim
}
);
const input = `(1, "22 foo bar "double quotes" here ", '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
- Lorem ipsum dolor '' sit amet - ''''
Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
Integer sit amet lacus ornare massa feugiat tempus.
', null, null);`;
const output = input.replace(
/(["'])([\s\S]*?)\1(?=[,)])/g,
(_, delim, content) => {
const pattern = new RegExp(delim, 'g')
return delim + content.replace(pattern, '\\' + delim) + delim
}
);
console.log(output);
的意思是:
/(["'])([\s\S]*?)\1(?=[,)])/g
匹配并捕获定界符(["'])
-重复任何字符,直到获得:([\s\S]*?)
-分隔符,后跟\1
-逗号或(?=[,)])