正则表达式在单引号内转义双引号 [英] Regular Expression to escape double quotes inside single quotes

查看:278
本文介绍了正则表达式在单引号内转义双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个转义或捕获(如果尚未转义)的正则表达式全部双引号字符 INSIDE 一个引用的字符串,然后转换开头的单引号双引号!

I would need a regular expression that escapes or captures (if not already escaped) ALL the double quote characters INSIDE a single quoted string and then convert the opening single quotes to double quotes!

我们正在重构在PHP和JS文件中有很多(我的意思很多!)单引号字符串的文件。他们唯一的共同点是字符串至少在一行中,并且以两种语言与=相关。

We are refactoring files that have a lot (and i mean a lot!) of single quoted strings in either PHP and also JS files. The only thing they have in common is that the strings are at least in one line and are concated with = in both languages.

我举一个例子(例子是丑陋的遗留代码,所以不要判断它,我已经做了这个 :)
我们有一个这样开始的文件:

I give an example (the example is ugly legacy code so dont judge it please, i already did this :) ) We have a file that starts like this:

var baseUrl = $("#baseurl").html();
var head = '<div id="finishingDiv" style="background-image:url({baseUrl}css/userAd/images/out_main.jpg); background-repeat: repeat-y; ">'+
'<div id="buttonbar" style="width:810px; text-align:right">';

我希望它像这样:

var baseUrl = $("#baseurl").html();
var head = "<div id=\"finishingDiv\" style=\"background-image:url({baseUrl}css/userAd/images/out_main.jpg); background-repeat: repeat-y; \">" +
"<div id=\"buttonbar\" style=\"width:810px; text-align:right\">";

如您所见,正确的双引号字符串不会被触动。

As you see the correct double quote strings are not touched.

所以我的基本问题:我如何捕获某个开始和结束字符之间的一种(在我的情况下是字符))的所有字符(在我的情况下是字符')。

So my basic question: How do i capture all characters of one kind (in my case the character " ) between a certain start and end character (in my case the character ' ).

这个正则表达式'。*()。*''[^'] *()[ ^'] *'每次匹配只会捕获一个如果需要多个步骤也可以,它应该只是工作。
我会很高兴任何解决方案,IDE具体,具体语言或特定于shell的功能,这是非常有用的。

This regex '.*(").*' or '[^']*(")[^']*' just captures always one " for me per match. If if needs more than one step its also ok, it should just work. I would be happy of any solution, IDE specific, language specific or shell specific, that acutally works.

请帮助,绝望,非常感谢/ p>

Please help, im desperate, thanks a lot

推荐答案

最大的问题是要确定所有字符串的位置,因为您无法解析所有的JS或PHP与正则表达式。但是,如果我假设您不关心评论,这个Ruby代码将会捕获大多数情况(但您应该查看其输出):

The biggest problem is going to be figuring out where all the strings are, since you can't parse all of JS or PHP with a regex. However, if I assume that you don't care about comments, this Ruby code will catch most cases (but you should review its output):

#!/usr/bin/ruby -p

gsub!(/'((?:[^\\']|\\[\\'])+)'/) do |m|
  %Q{"#{$1.gsub("\\'","'").gsub(/\\[^\\]/) { "\\#{$0}" }.gsub('"','\\"')}"}
end

此代码采用stdin /文件参数内容中的任何内容,查找单引号字符串(考虑到可能存在的 \\ \'),然后,为了替换,在匹配的字符串内运行一系列替换(清理反斜杠等)。结果打印到stdout。如果您想要更自动化的方法,请将第一行替换为#!/ usr / bin / ruby​​ -pi.bak ;那么,无论什么文件参数都被替换,就会在其上运行破坏性就地。旧文件保留额外的 .bak 扩展名。

This code takes whatever's presented on stdin / the contents of the file arguments, finds a single-quoted string (taking into account the possible presence of \\ and \'), and then, for its replacement, runs a series of substitutions within the matched string (sanitizing backslashes, etc.). The result is printed to stdout. If you want a more automated approach, replace the first line with #!/usr/bin/ruby -pi.bak; then, whatever file arguments are presented have the substitution run on them destructively in-place. The old files are kept with an additional .bak extension.

要运行此代码,如果没有使用Ruby之前:将其保存为任何内容,例如 fix-sq.rb ;运行 chmod + x fix-sq.rb ;然后运行 ./ fix-sq.rb file1 file2 file3

To run this code, if you haven't used Ruby before: save it as anything, such as fix-sq.rb; run chmod +x fix-sq.rb; and then run ./fix-sq.rb file1 file2 file3.

这篇关于正则表达式在单引号内转义双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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