const regex = /(\|\s*(logo|image|screenshot|race_logo|track map)\s*=\s*)(File:|Image:)?([^\[\||<\n]*)((\|\s*\d{1,4}px(\?)?|\|\s*thumb))((\|\s*\d{1,4}px(\?)?|\|\s*thumb))?(?(?=\|[A-z]*\s*=)|(?(?=\|(\w*\W*)*)(\|[^\|\n]*)?)?)/gi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(\\|\\s*(logo|image|screenshot|race_logo|track map)\\s*=\\s*)(File:|Image:)?([^\\[\\||<\\n]*)((\\|\\s*\\d{1,4}px(\\?)?|\\|\\s*thumb))((\\|\\s*\\d{1,4}px(\\?)?|\\|\\s*thumb))?(?(?=\\|[A-z]*\\s*=)|(?(?=\\|(\\w*\\W*)*)(\\|[^\\|\\n]*)?)?)', 'gi')
const str = `| image = [[File:Central Asia (orthographic projection).svg|200px|Map of Central Asia]]
|Image=|150px?
}}]] – [[Template:POTD/2012-06-09|{{POTD texttitle
|image=Jack-in-cube solid model, light background.gif
|thumb=Jack-in-cube solid model, light background frame.png
|size=180
|name = Reggae
| Image = File:Bob-Marley.jpg|thumb|231px|
| caption = Popular reggae artist [[Bob Marley]] in 1980
{{Infobox officeholder
|image = Karenskiy AF 1917.jpg|230px
|nationality = Russian
{{Infobox officeholder
|image = Karenskiy AF 1917.jpg|230px|thumb
|nationality = Russian
{{Infobox officeholder
|image = Karenskiy AF 1917.jpg|thumb
|nationality = Russian
{{Infobox officeholder
|image = Karenskiy AF 1917.jpg|230px|nationality = Russian
|country=IND
|image= National Highway 2 (India).png|300px|Road map of India with NH 19 highlighted in solid blue color
|type=NH
|image = File:April 14, 2012 Marquette, Kansas EF4 tornado.JPG|309px
{{Infobox football club |
clubname = Slovácko |
image = Image:1FC Slovacko.png|150px|Logo |
fullname = 1. FC Slovácko A.S. |
| image = <imagemap>
File:Primates - some families.jpg|300px`;
const subst = `$1$4`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions