VS Code Find and Replace across files using Regex to correct error in number of quotes appearing on a particular line

Viewed 59

EDIT:
While @Wiktor Stribiżew's answer helped me resolve both my issues (thanks again!), I came across a 3rd issue, on the lines of the 2nd one.

Error:

"Image" : "'Motorola_About'

There is an unwanted single quote before the 'Motorola_About' thread and the closing quote needs to changed to double. There is white-space before and after the colon (:) which needs to be retained.

I tried the below codes on the lines of the answer given for the previous issue FIND:

(" : ")"([^']*_About[^']")
("/s:/s")"([^']*_About[^']")

REPLACE:

$1$2

Both approaches did not work. Would appreciate any help or assistance in telling me where I'm going wrong.

TRAIL: I am trying to use VS Code's (Ver 1.55.2) 'Replace in Files' to correct an error in number of quotes appearing on a particular line across multiple files. In both the error cases, there is an additional single quote (') that has appeared in the same position (before the image name) in error, which I need to remove across all the files.

Error type 1:

html file1 Line 50: 'ImageName':''Microsoft_About',
html file2 Line 51: 'ImageName':''Motorola_About',
html file3 Line 53: 'ImageName':''Apple_About',

..and so on across 450 odd files

Error type 2:

html file9 Line 50: "ImageName":"'Microsoft_About",
html file12 Line 51: "ImageName":"'Motorola_About",
html file15 Line 53: "ImageName":"'Apple_About",

..and so on across another 350 odd files

I tried using the following Regex code in 'Replace in Files': For Error1:

FIND: ([''])([a-zA-Z0-9,.-]+_)(_About')
REPLACE: $2_About' 

For Error2:

FIND: (["'])([a-zA-Z0-9,.-]+_)(_About')
REPLACE: $2_About' 

It is not working. I'm new to Regex and not familiar with the codes, so would highly appreciate any help and advice with this.

2 Answers

For the first set of files you can use

Find: '('[^']*)(_About')
Replace: $1$2

See the regex demo. For the second one, you may use

Find: (":")'([^"]*_About")
Replace: $1$2

See this regex demo.

Details:

  • '('[^']*)(_About') - a ' is matched, then a ' and zero or more chars other than ' are captured into Group 1 ($1) and then _About' is captured into Group 2 ($2).
  • (":")'([^"]*_About") - ":" is first captured into Group 1 ($1), then a ' is matched, and then any zero or more chars other than " and _About" is captured into Group 2.

The matches are replaced with appropriate group value(s).

I used one regex to match both cases with the same replace statements. Regex Explanation

const regex = /('|")(.*?)(?:'|"):(?:'|")+(.*?)(?:'|")/;

const error1 = [`'ImageName':''Apple_About'`, `'ImageName':''Motorola_About'`, `'ImageName':''Microsoft_About'`];
const error2 = [`"ImageName":"'Microsoft_About"`,`"ImageName":"'Motorola_About"`, `"ImageName":"'Apple_About"`];

console.log(error1.map(err => err.replace(regex, '$1$2$1:$1$3$1')));
console.log(error2.map(err => err.replace(regex, '$1$2$1:$1$3$1')));

Related