Regular expression to avoid double quotes inside single quotes

I will need a regex that escapes or captures (if not escaped) ALL double-quote characters INSIDE a single quote and then converts open single quotes to double quotes!

We are refactoring files that have many (and I mean many!) Single quotes in PHP or JS files. The only thing they have in common is that the lines are in at least one line and are summarized in = in both languages.

I will give an example (an example is an ugly outdated code, so don’t judge it, I already did it :)) We have a file that starts as follows:

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

and I want it to look 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\">";

, .

, : ( ) ( ).

'.*(").*' '[^']*(")[^']*' " ". , , , . , IDE, , .

, , , .

+1
2

, , , JS PHP . , , , Ruby ( ):

#!/usr/bin/ruby -p

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

, stdin/ , ( \\ \'), ( ..). . , #!/usr/bin/ruby -pi.bak; , , . .bak.

, Ruby: , fix-sq.rb; chmod +x fix-sq.rb; ./fix-sq.rb file1 file2 file3.

+1

, . , , (".*)+ . : " : , ."

0

Source: https://habr.com/ru/post/1772557/


All Articles