User:Prof. Squirrel/common.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
var wikEdConfig = {};
wikEdConfig.comboPresetOptions = {};
wikEdConfig.comboPresetOptions.summary = [
	'added {{conUse British English}} tag (as per [[MOS:ENGVAR]])',
	'Added ref and removed {{citation needed}}',
	'adding citations',
	'added {{copyedit}}',
	'added stub tag',
	'added wikilinks',
	'added wikilinks and removed {{underlinked}}',
	'copyediting',
	'fixed dead links',
	'fixed refs',
	'formatting',
	'formatted refs',
	'formatted refs (using [[Zotero]])',
	'minor copyediting',
	'minor formatting',
	'minor spacing',
	'pruned list of external links',
	'removed {{copyedit}} tag',
	'REMOVED COPYVIO',
	'Removed unsourced statement',
	'removed duplicate wikilinks ([[WP:REPEATLINK]])',
	'removed extraneous info',
	'removed irrelevant info',
	'removed {{lead too short}}, since article is a stub',
	'renamed/organized sections',
	'resolved {{citation style}} and removed tag',
	'rewording/clarifing',
	'some rewording for conciseness',
];
// wikEdConfig.comboPresetOptions.find = [
// 	' +', ' <ref'
// ];
// wikEdConfig.comboPresetOptions.replace = [
// 	' ', '<ref'
// ];

importScript('User:Dr_pda/prosesize.js');
importScript('User:Ucucha/duplinks2.js');
importScript('User:Bility/copySectionLink.js');

importScript('User:Prof. Squirrel/dates.js'); //Stolen/modified from Ohconfucius
//importScript('User:Ohconfucius/script/EngvarB.js');
//importScript('User:Ohconfucius/script/Common Terms.js');
//importScript('User:Ohconfucius/script/Sources.js'); // [[User:Ohconfucius/script/Sources.js]]

importScript('User:GregU/randomlink.js');
importScript('User:MC10/stubtagtab.js');
//importScript('User:AndyZ/peerreviewer.js');
//mw.loader.load('//en.wikipedia.org/w/index.php?title=User%3AAndyZ%2Fpeerreviewer.js&action=raw&ctype=text/javascript'); // [[User:AndyZ/peerreviewer.js]]
//	initMsg_PR='';
//	endMsg_PR='';
//importScript('User:Cameltrader/Advisor.js');
//importScript('User:Ais523/bracketmatch.js');

//Add extra links to navbar at top
$('#pt-mytalk').after('<li id="pt-notes"><a href="http://en.wikipedia.org/w/index.php?title=User:Prof._Squirrel/Notes">Notes</a></li>' );
$('#pt-mytalk').after('<li id="pt-wip"><a href="http://en.wikipedia.org/w/index.php?title=User:Prof._Squirrel/WIP">WIP</a></li>' );
$('#pt-preferences').before('<li id="pt-mysandbox1"><a href="http://en.wikipedia.org/w/index.php?title=User:Prof._Squirrel/sandbox">Sandbox 1</a></li>' );
$('#pt-preferences').before('<li id="pt-mysandbox2"><a href="http://en.wikipedia.org/w/index.php?title=User:Prof._Squirrel/sandbox_2">Sandbox 2</a></li>' );
$('#pt-preferences').before('<li id="pt-mysandbox3"><a href="http://en.wikipedia.org/w/index.php?title=User:Prof._Squirrel/sandbox_3">Sandbox 3</a></li>' );
$('#pt-preferences').after('<li id="pt-vector"><a href="http://en.wikipedia.org/w/index.php?title=User:Prof._Squirrel/common.js&action=edit">common.js</a></li>' );

//importScript("User:Ohconfucius/script/MOSNUM_utils.js");

/**
 * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
 * @see https://meta.wikimedia.org/wiki/TemplateScript
 * @update-token [[File:pathoschild/templatescript.js]]
 */
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
pathoschild.TemplateScript.add([
	{
		name: 'Edit my regexes ↗',
		script: function(editor) {
			window.open(mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=User:' + mw.config.get('wgUserName') + '/common.js&action=edit');
		}
	},
	{
		name: '• All line breaks',
		script: function(editor) {
			editor
				.replace(/\n/g, " ")
				.clickDiff();
		}
	},
	{
		name: '• Double spaces',
		script: function(editor) {
			editor
				.replace(/  /g, " ")
				.clickDiff();
		}
	},
	{
		name: '• Extra line breaks',
		script: function(editor) {
			editor
				.replace( /\n{2,}(?=\n)/gi, " ")
				.clickDiff();
		}
	},
	{
		name: '• Notes #1',
		script: function(editor) {
			editor
				.replace( /({{.*)/gi, "{{/Template \n |citation=$1 \n |copy=<nowiki><ref>$1</ref></nowiki>\n |notes=\n |col=yes\n}}")
				.clickDiff();
		}
	},
	{
		name: '• Notes #2',
		script: function(editor) {
			editor
				.replace( /\n\n\*/gi, "\n*")
				.replace( '->', "→")
				.clickDiff();
		}
	},
	{ name: '• Fix refs (Ohconfucius)',
	  script: function(editor) { 
	  	
	  	editor
		.replace(/ <ref/gi, '<ref')
	//	.replace(/title\s*=\s*(.+?)\.\s*\|/gi, 'title=$1 |') //gets rid of period after title
		
		//ALL THIS IS STOLEN
		//For bare URLs
		.replace(/([^>\*\s])\s?(\[https?:\/\/[^\s\[\]]*\])(?![ ]*[<\-]+)/gi, '$1<ref>$2</ref>')
			
		// removing artefacts within fields
		.replace(/(\|\s?author\s*\=\s*)(?:by |)(?:[^|]+staff|(?:staff |)(?:reporter|writer)s?|)[ ]*(?=[|}\n])/gi, '')
		.replace(/(\|\s?author\s*\=\s*)([A-Z][a-z]*(?: [A-Z][a-z]*)*) (?:(?:wire |)staff|(?:staff |)(?:reporter|writer)s?)[ ]*(?=[|}\n])/gi, '$1$2')
	    .replace(/\|[ ]*last=(Reporter|staff)[ ]*\|[ ]*first=[^|\{\}]*(?=[\|{}])=/gi, '')
	    .replace(/\|[ ]*(?:first|last)=(?:staff |)(?:reporter|writer)[ ]*(?=[\|{}])=/gi, '')
		.replace(/(\|\s?access-?date\s*\=\s*)(?:accessed|retrieved)(?: by| on|):?[ ]*(\d)/gi, '$1$2')
		.replace(/(\|\s?access-?date\s*\=\s*\d{4}\s?)(?=[|}])/gi, '')
		.replace(/(\|\s?work\s*\=\s*)(?:article|interview|review) ?(?=[|}\n])/gi, '$1')
		.replace(/(\|\s?volume\s*\=\s*)vol(?:ume|\.?)[ ]*(\d)/gi, '$1$2')
		.replace(/(\|\s?pages?\s*\=\s*)(?:pages?|p[gp]?\.?)[ ]*(\d)/gi, '$1$2')
		.replace(/ [-–] 每日明報 daily news/gi, '')
		.replace(/(\|\s?title\s*\=\s*)Login ?(?=[|}\n])/gi, '$1ACTUAL ARTICLE TITLE BELONGS HERE')
		.replace(/(\|\s?publisher=)\|\s?via\s*\=\s*(\w+)(?=\s*[|}])/gi, '$1$2')
				
		//citation template fixes
		.replace(/(\|\s?)published\s?=/gi, '$1publisher=')
	    
	    // removing  copyright assertion
		.replace(/(\|\s?publisher\s*\=\s*)(?:\[\[copyright(?:\|©|)\]\])\s?/gi, '$1')
		.replace(/(\|\s?publisher\s*\=\s*)(?:©|copyright)\s?/gi, '$1')
		.replace(/(\|\s?title\s*\=\s*[^|}]+?)[-‒–—―] *([^|}]+?)(\|\s?(?:publisher|work)\s*\=\s*\2)/gi, '$1$3')
		.replace(/(\|\s?title\s*\=\s*([^|}]+?))[-‒–—―] *([^|}]+?\|\s?(?:publisher|work)\s*\=\s*\2)/gi, '$1$3')
			
		// misused 'date' parameter
		.replace(/(\{\{\s?cit[ae][^}]+)\|\s?date(\s?=\s?[12]\d{3}\s?[|}])/gi, '$1|year$2')
	
	    // removing toggles and redundant quote marks
		.replace(/(\|\s?(?:agency|author|newspaper|work|journal|publisher)\s*\=\s*)\'\'([^|}]+)\'\'(?=\s*[\}\|])/gi, '$1$2') //without link
		.replace(/(\|\s?(?:agency|author|newspaper|work|journal|publisher)\s*\=\s*)\'\'(\[\[(?:[^\|]+\||)[^\|\]]+\]\])\'\'(?=\s*[\}\|])/gi, '$1$2')  //with link
	    .replace(/(\|\s?title\s*\=\s*)\'&#39;([^\|\{\}]+)\'&#39;/gi, '$1$2') //removing &#39; in titles
	    .replace(/(\|\s?publisher\s*\=\s*)\(([^\|\{\}]+)\)/gi, '$1$2') //removing parenthetical publishers	

		// reordering 'work' and 'publisher' (first run - see second run in cleanup function)
		 .replace(/(\|\s?publisher\s*\=\s*(?:\[\[[^<{}\]]*\]\]|[^{}\|\}<>]*))(\s?\|[^}<>]*|)(\|\s?(?: journal|newspaper|magazine|periodical|website|work)\s*\=\s*(?:\[\[[^<{}\]]*\]\]|[^{}\|\}<>]*))(?=[\s\.]*[|}])/g, '$3$1$2')
		 .replace(/(\|\s?website\s*\=\s*(?:\[\[[^<{}\]]*\]\]|[^{}\|\}<>]*))(\s?\|[^}<>]*|)(\|\s?(?: journal|newspaper|magazine|periodical|work)\s*\=\s*(?:\[\[[^<{}\]]*\]\]|[^{}\|\}<>]*))(?=[\s\.]*[|}])/g, '$3$1$2')

		// removing identical/similar entries in 'work' and 'publisher', and in 'work' and 'website' (different default vs [post] cleanup rules)
	     .replace(/\|\s?work\s*\=\s*([^=|}\[<>]*)(\|[^}<>]*|)\|\s?(?:publisher|website)\s*\=\s*(?:\1|\[\[\1\]\])\.?(?=\s*[|}])/g, '| publisher=$1$2') //unlinked work
	     .replace(/\|\s?work\s*\=\s*\[\[([^<|\]]*)\]\](\|[^}<>]*|)\|\s?(?:publisher|website)\s*\=\s*(?:\1|\[\[\1\]\])\.?(?=\s*[|}])/g, '| publisher=$1$2') //unpiped work
	     .replace(/\|\s?work\s*\=\s*(\[\[(?:[^<|\]]*)\|([^}<>]*)\]\])(\|[^}<>]*|)\|\s?(?:publisher|website)\s*\=\s*(\1|\2)\.?(?=\s*[|}])/g, '| publisher=$1$3') //piped work

	     .replace(/\|\s?publisher\s*\=\s*([^=|}\[<>]*)(\|[^}<>]*|)\|\s?publisher\s*\=\s*(?:\1|\[\[\1\]\])\.?(?=\s*[|}])/g, '| publisher=$1$2') //unlinked work
	     .replace(/\|\s?publisher\s*\=\s*\[\[([^<|\]]*)\]\](\|[^}<>]*|)\|\s?publisher\s*\=\s*(?:\1|\[\[\1\]\])\.?(?=\s*[|}])/g, '| publisher=$1$2') //unpiped work
	     .replace(/\|\s?publisher\s*\=\s*(\[\[(?:[^<|\]]*)\|([^}<>]*)\]\])(\|[^}<>]*|)\|\s?publisher\s*\=\s*(\1|\2)\.?(?=\s*[|}])/g, '| publisher=$1$3') //piped work

		 // remove redundant parentheses and templates from dm and md dates (equivalents also exists in Mosnum script)
		.replace(/(=[ ]*)\(([^()|{}])\)/gi, '$1$2')
		
		// removing corporate designation
		.replace(/(\|\s?(?:author|publisher|work)\s*\=\s*[^\[|}]{1,40}),? (?:Inc|LL[CP]|Ltd|PLC|SA)\.?(?=[ ]*\|\})/gi, '$1')
		
		// correcting yahoo! corporate designation
		.replace(/(\|\s?(?:publisher|work)\s*\=\s*Yahoo)(?: |)(Finance|Green|Kids|Music|News|)(?=[ ]*\|\})/gi, '$1! $2')

		// removing unnecessary quote marks
		.replace(/(\|\s?title\s*\=\s*)["“]([^\|]+)["”](?=\s?[|}])/gi, '$1$2')
		.replace(/(\|\s?title\s*\=\s*)['‘]([^\|'’]+)['’](?=\s?[|}])/gi, '$1$2')
		
		// repl double 'in-title' quote marks with single quotes
		.replace(/(\|\s?title\s*\=\s*[\w ]* )["“]((?:\w[\w]* )+(?:\w[\w]*))["”]([^\|]+|)(?=\s?[|}])/gi, '$1\'$2\'$3')
		
		// adjust for possibly incorrectly input title
         .replace(/(\|\s?title\s*\=\s*)([^\|\}<>]*)(\s?\|[^}<>]*|)\|\s?(publisher|work)\s*\=\s*(?:\1|\[\[\1\]\])(?=\s*[|}])/g, '$1ACTUAL ARTICLE TITLE BELONGS HERE! |$4=$2$3')
         .replace(/(\|\s?title\s*\=\s*)(\w+\.com)(?=\s?[=|{}])/gi, '$1ACTUAL ARTICLE TITLE BELONGS HERE! |publisher=$2')
 
		 // rem misplaced punctuation
         .replace(/(<ref[^>]*>[^<]+?[\]\.\},;–]\s*\'\'[\w-]*(?: [\w-]*){0,3})(\.com|)([;,\.])(\'\')(?=[^<]*?<\/ref>)/gi, '$1$2$4$3')
         .replace(/([\w]+)\.(['"]\])[ ]/gi, '$1$2. ')    //LQ for titles

		 // removing blank or redundant parameters
         .replace(/(?:\|[ ]*(?:access-?date|agency|archive-?(?:date|url)|arxiv|asin|at|author(-?link|-mask|-name-separator|-separator|\d|\d-link|link\d?|)|bibcode|chapter|chapter-url|coauthors?|contribution(?:-url|)|date|deadurl|display-authors|doi|doi-inactive|doibroken|edition|editor(?:-first|-last|-link|\d|\d-first|\d-last|\d-link)|(?:first|last)\d?|format|id|is[bs]n|issue|jfm|journal|jstor|language|lay(?:date|source|summary)|lccn|location|magazine|day|month|mr|newspaper|nopp|oclc|ol|origyear|osti|others|pages?|periodical|place|pm[cd]|pmid|postscript|publication(?:-date|-place)|publisher|quote|ref|rfc|separator|series|ssrn|trans_title|type|url|volume|via|work|year|zbl)[ ]*=[\s]*)(?=[}|])/gi, '')
         .replace(/(?:\|[ ]*(began|ended)[ ]*=[^}|]*)(?=[}|])/gi, '')
         .replace(/(\|\s?work\s?\=\s?)\[\[iTunes\]\] Archive\s/gi, '| publisher=[[iTunes Store]]')

	    //rem underlining within certain fields
		.replace(/(\|\s?(?:journal|newspaper|periodical|publisher|work)\s*\=\s*)<u>([^|}]*)<\/u>/gi, '$1$2')
	
	    //rem redundant top-level domains (.com, .net, .org), strip "www"
	    .replace(/(\|\s?(?:journal|newspaper|periodical|publisher|work)\s*\=\s*)(\[\[[^\[\]\}]*\]\])\.(?:biz|com|net|org|co\.uk|(?:com\.|)[a-z]{2})(?=\s*[|}])/gi, '$1$2')
	
	    //rem duplicated publishers in separate fields (pre)
			.replace(/(?:[‒–—―]+|&#124;)\s*(?:The |)([^\|\}&]{3,})(?:\.com|)\s*(\|\s?(?:agency|publisher|work)\s*\=\s*)\1(?=\s*[|}])/gi, '$2$1')
			.replace(/(?:[‒–—―]+|&#124;)\s*([^\|\}&]{3,})(?:\.com|)\s*(\|\s?(?:agency|publisher|work)\s*\=\s*)(The |)\1(?=\s*[|}])/gi, '$2$3$1')
	
	    //'work' and its alias (pre)
	         .replace(/(\|[ ]*?newspaper[ ]*=[^\|}]*(?:\|[^\{\}]*|))(?:\|[ ]*?work[ ]*=[^|}]*)(?=[|}\n])+/gi, '$1')

    	//rem linking within 'location' field
         .replace(/(\|[ ]*?(?:location|place)=[ ]*?)\[\[ ?(Abkhazia|Afghanistan|Albania|Algeria|Andorra|Angola|Antigua and Barbuda|Argentina|Armenia|Australia|Austria|Azerbaijan|(?:The |)Bahamas|Bahrain|Bangladesh|Barbados|Belarus|Belgium|Belize|Benin|Bhutan|Bolivia|Bosnia and Herzegovina|Botswana|Brazil|Brunei|Bulgaria|Burkina Faso|Burma|Burundi|Cambodia|Cameroon|Canada|Cape Verde|Central African Republic|Chad|Chile|(?:(?:People's |)Republic of |)China|Colombia|Comoros|(?:Democratic |)Republic of (?:the |)Congo|Costa Rica|Côte d'Ivoire|Croatia|Cuba|Cyprus|Czech Republic|(?:Kingdom of |)Denmark|Djibouti|Dominica|Dominican Republic|East Timor|Ecuador|Egypt|El Salvador|Equatorial Guinea|Eritrea|Estonia|Ethiopia|Fiji|Finland|France|Gabon|Georgia \(country\)|Germany|Ghana|Greece|Greenland|Grenada|Guatemala|Guinea|Guinea-Bissau|Guyana|Haiti|Honduras|Hungary|Iceland|India|Indonesia|Iran|Iraq|(?:Republic of |)Ireland|Israel|Italy|Jamaica|Japan|Jordan|Kazakhstan|Kenya|Kiribati|North Korea|South Korea|Kosovo|Kuwait|Kyrgyzstan|Laos|Latvia|Lebanon|Lesotho|Liberia|Libya|Liechtenstein|Lithuania|Luxembourg|(?:Republic of |)Macedonia|Madagascar|Malawi|Malaysia|Maldives|Mali|Malta|Marshall Islands|Mauritania|Mauritius|Myanmar|M[ée]xico|(?:Federated States of |)Micronesia|Moldova|Monaco|Mongolia|Montenegro|Morocco|Mozambique|Nagorno-Karabakh|Namibia|Nauru|Nepal|(?:Kingdom of the |)Netherlands|Holland|New Zealand|Nicaragua|Niger|Nigeria|Northern Cyprus|Norway|Oman|Pakistan|Palau|Palestine|Panama|Papua New Guinea|Paraguay|Peru|Philippines|Poland|Portugal|Qatar|Romania|Russia|Rwanda|SADR|Saint Kitts and Nevis|Saint Lucia|Saint Vincent and the Grenadines|Samoa|San Marino|São Tomé and Príncipe|Saudi Arabia|Senegal|Serbia|Seychelles|Sierra Leone|Singapore|Slovakia|Slovenia|Solomon Islands|Somalia|Somaliland|South Africa|South Ossetia|Spain|Sri Lanka|Sudan|Suriname|Swaziland|Sweden|Switzerland|Syria|Taiwan|Tajikistan|Tanzania|Thailand|Timor Leste|(?:The |)Gambia|Togo|Tonga|Transnistria|Trinidad and Tobago|Tunisia|Turkey|Turkmenistan|Tuvalu|Uganda|Ukraine|United Arab Emirates|United Kingdom|United States|Uruguay|Uzbekistan|Vanuatu|Vatican City|Venezuela|Vietnam|Yemen|Zambia|Zimbabwe)[ ]?\|[ ]?(?:\w{2,3})\]\]/gi, '$1$2')
         .replace(/(\|[ ]*?(?:location|place)=[ ]*?)\[\[(?:[^\|\]]+?\||)([\w\s\,]+?)\]\](?:(,? )\[\[(?:[^\|\]]+?\||)([\w\s\,]+?)\]\])(?:(,? )\[\[(?:[^\|\]]+?\||)([\w\s\,]+?)\]\]|)(?=[ ]?[|}])/gi, '$1$2$3$4$5$6')
         .replace(/(\|[ ]*?(?:location|place)=[ ]*?)\[\[(?:[^\|\]]+?\||)([\w\s\,]+?)\]\](?:(,? )\[\[(?:[^\|\]]+?\||)([\w\s\,]+?)\]\]|)(?=[ ]?[|}])/gi, '$1$2$3$4')

		// removing english language parameters
	    .replace(/[ ]?\{\{en[- ]icon\}\}/gi, "")
	    .replace(/\|[ ]*?language[ ]*?=[ ]*?en(?:-\w{2}|glish|)(?=[\s\|]+)/gi, "")
	    .replace(/(\|[ ]*?language[ ]*?=[ ]*?)\{\{(\w{2})(?:[- ](?:icon|UK|GB|US)|)\}\}/gi, "") // removing icon template from within "|language=" parameter
		
		// eliminating time of day
	    .replace(/(\|[ ]*author[ ]*=[ ]*)(?:posted|published)(?: by| on|)[\s:](?=\s*\w)/gi, "$1")
	    .replace(/(\|[ ]*(?:date|archive-?date|access-?date|author|first|last)[ ]*=[ ]*)[0-2]?\d:[0-5]\d(?:[ ]|&nbsp;)(?:[ap]m ?|[ap]\.m\. |[A-Z]{1,2}T|UTC)[\.,]?[ ]?/gi, "$1")

		 // eliminating days of the week
	    .replace(/(\|[ ]*(?:date|archive-?date|access-?date|author)[ ]*=[ ]*)(?:(?:Mon|Tues?|Wed|Thur?|Fri|Sat|Sun)(?:[\.,]|day)?)\s/gi, "$1")

    	//'Accessed'/'Obtained' -> 'Retrieved'
		.replace(/(?:[\.,;][ ]*(?:url |link |last |)(?:Retrieved|Accessed|Obtained))(?: on(?:line|)|):? (@month|@dd|@yyyy)(?=\D)/gi, '. Retrieved $1')
    	.replace(/(\w|\])(?:[ ]*(?:url |link |last |)(?:Retrieved|Accessed|Obtained))(?: on(?:line|)|):? (@month|@dd|@yyyy)(?=\D)/gi, '$1. Retrieved $2')	
		
		// newspapers
		.replace(/(\[\[)(?:(?:British|English|London) Sun|Sun on Sunday|The Scottish Sun|(?:The |)Sun (?:\((?:British |)newspaper\)|\(tabloid\)|\(UK newspaper\)|\(UK\)|Newspaper|on Sunday|Online)|Thesun\.co\.uk)(?=\|)/gi, '$1The Sun (United Kingdom)')
         .replace(/(\[\[)Daily Star \((?:British|UK)\)(?=\|)/gi, '$1Daily Star (United Kingdom)')
         .replace(/(\[\[Metro)(?: \(Associated Metro Limited\)| \(Associated Newspapers\)| \(London newspaper\)| \(free London newspaper\)| UK| newspaper London| newspaper UK)(?=\|)/gi, '$1 (British newspaper)')
         .replace(/(\[\[)(?:Calcutta Telegraph|The Telegraph \((?:kolkatt?a|India)\)|(?:The |)Telegraph India|Telegraphindia\.com)(?=\|)/gi, '$1The Telegraph (Calcutta)')
         .replace(/(\[\[)Dawn(?:, Karachi| newspaper|\.com| \((?:Newspaper|Pakistan)\))(?=\|)/gi, '$1Dawn (newspaper)')
         .replace(/(\[\[The Pioneer)(?:, Karachi| newspaper| \((?:indian newspaper)\))(?=\|)/gi, '$1 (Indian newspaper)')
         .replace(/(\[\[)dailypioneer.com(?=\|)/gi, '$1The Pioneer (Indian newspaper)')
         .replace(/(\|)(Sport \()(newspaper\))(?=\||\]\])/g, '$1$2Spanish $3')  //dab moved December 2012
         .replace(/(=[ ]*\[\[)(?:[BE]SPN ?(?:USA|HD|Network|the ocho|\(United States\))|E.S.P.N.|(?:The |)Entertainment (?:and |)Sports Programming Network)(?:\|[\w, ]*)(?=\]\])/gi, '$1ESPN')
         .replace(/(\[\[)Sky ?(?:\((?:British|UK)\)|UK|United Kingdom)(?=\|)/gi, '$1Sky (United Kingdom)')
		
         .replace(/(?:agency|journal|newspaper|periodical|publisher|website|work)(\s?=\s?\[\[)(?:MTV (?:[A-Z]\w*|\([^\)\]]*\)))\|[^\)\]]*(?=\]\])/gi, 'publisher$1MTV')
		
		//unwinding of unnecessary pipes
         .replace(/\[\[Public Broadcasting Service\|(PBS\]\])/gi, '[[$1')
	
		// removing publishers less well-known than their titles
         .replace(/(\|\s?publisher\s?\=MTV\|\s?)publisher\s*\=\s*(?:MTV Networks(?: \(Viacom\)|)|Viacom)/gi, '$1=')

         .replace(/(?:\|publisher=Turner Sports Interactive, Inc)\.? ?\|publisher=(NBA)(?= ?\|)/gi, '$1')
         .replace(/(\|publisher=NBA) ?\|publisher=(?:Turner Sports Interactive, Inc)\.?(?= ?\|)/gi, '$1')
         .replace(/\[\[Jann? Wenner\|Wenner Media\]\](?= ?\|)/gi, '')
		
		// removing publishers for periodicals
         .replace(/\|publisher=\[\[(?:PMC \(company\)\||)(?:PMC|Penske Media Corporation)\]\](?=[\s\.]*[|}])/gi, '')
         .replace(/(?:\|\s?(newspaper|work|publisher)\s*\=\s*\[?\[?(?:Hachette Filipacchi Médias\||)Hachette Filipacchi(?: \(UK\) Ltd.?| UK|)\]?\]?)(?=[\s\.]*[|}])/gi, '')
         .replace(/(?:\|\s?(newspaper|work|publisher)\s*\=\s*(ACP Magazines|The Herald and Weekly Times|John Fairfax (and Sons Ltd\.?|Holdings)|Fairfax(?: Media(?: Limited|)| Digital| newspapers|)))(?=[\s\.]*[|}])/gi, '')
         .replace(/(?:\|\s?(newspaper|work|publisher)\s*\=\s*\[\[(ACP Magazines|The Herald and Weekly Times|John Fairfax (and Sons Ltd\.?|Holdings)|Fairfax(?: Media(?: Limited|)| Digital| newspapers))\]\])(?=[\s\.]*[|}])/gi, '')
         .replace(/(?:\|\s?publisher\s*\=\s*(Alexander Lebedev|American Media|Associated Newspapers|Bauer (?:Consumer Media|Media Group)|Cond[eé] Nast(?: Publications|)|Daily Mail and General Trust|Devin Laz[ae]rine|Dow Jones & Company|Future plc|(Guardian|Telegraph) Media Group|(?:Guardian|Independent) News (?:and|&) Media (?:Limited|Ltd\.|)|Hachette Filipacchi Médias|Hearst (?:Corporation|Magazines(?: UK|))|Herald Media|IGN Entertainment|Imdb Inc\.?|InterMedia Partners|IDG|IPC Media|Lee Enterprises|Media ?News Group|Mortimer Zuckerman|MTV Networks|News (?:Corporation|International|Limited)|Prometheus Global Media|Reed Business Information|Rovi Corporation|Trinity Mirror|Times Newspapers|Nielsen (?: Media Research|Business Media)|Viacom|Time(?: Warner ?|)))(,? Inc| LL[CP]| Ltd|Limited|)[\s\.]*(?=[|}\n])/gi, '')
         .replace(/\|\s?publisher\s*\=\s*(?:The |)(?:Deseret News Publishing|Dispatch Printing|E\. W\. Scripps|Evening Post Publishing|Forbes(?: Publishing|, Inc\.)|Gannett?|Irish Times Trust|(?:Jann Wenner|Wenner Media)|Johnson Publishing|Journal Communications|Mac Publishing|Media24|McClatchy|Nash holdings LLC|New York Times|Seattle Times|Star Tribune|Thomp?son(?:[- ]?Reuters)?(?: Corporation| Plc.?|)|Torstar|Time Inc\.|Times (?:Group|Publishing)|Tribune|Vox Media|Washington Post|World Publishing|Ziff Davis Media)(?: Co(?:mpany|\.)?)?(?=[\s\.]*[|}])/g, '')
         .replace(/\|\s?publisher\s*\=\s*(?:Cox|Halifax|North Jersey|Sun-Times|Tampa|Herald|Stephens|WEHCO|\w+) Media(?: Group(?:,? Inc\.)?| Berhad)?(?=[\s\.]*[|}])/g, '')

		//duplicate above with links //("Corporation" excluded - false positive with Australian Broadcasting Corporation)
         .replace(/(?:\|\s?publisher\s*\=\s*\[\[(Alexander Lebedev|American Media|Associated Newspapers|Bauer (?:Consumer Media|Media Group)|Cond[eé] Nast(?: Publications|)|Daily Mail and General Trust|Devin Laz[ae]rine|Dow Jones & Company|Future plc|(Guardian|Telegraph) Media Group|(?:Guardian|Independent) News (?:and|&) Media (?:Limited|Ltd\.|)|Hachette Filipacchi Médias|Hearst (?:Corporation|Magazines(?: UK|))|Herald Media|IGN Entertainment|Imdb Inc\.?|InterMedia Partners|IDG|IPC Media|Lee Enterprises|Media ?News Group|Mortimer Zuckerman|MTV Networks|News (?:Corporation|International|Limited)|Prometheus Global Media|Reed Business Information|Rovi Corporation|Trinity Mirror|Times Newspapers|Nielsen (?: Media Research|Business Media)|Viacom|Time(?: Warner ?|)))(,? Inc| LL[CP]| Ltd|Limited|)(?:\|[^\]\}]*|)\]\][\s\.]*(?=[|}\n])/gi, '')
         .replace(/\|\s?publisher\s*\=\s*\[\[(?:The |)(?:Deseret News Publishing|Dispatch Printing|E\. W\. Scripps|Evening Post Publishing|Forbes(?: Publishing|, Inc\.)|Gannett?|Irish Times Trust|(?:Jann Wenner|Wenner Media)|Johnson Publishing|Journal Communications|Mac Publishing|McClatchy|Nash holdings LLC|New York Times|Seattle Times|Star Tribune|Thomp?son(?:[- ]?Reuters)?(?: Corporation| Plc.?|)|Torstar|Time Inc\.|Times Publishing|Tribune|Vox Media|Washington Post|World Publishing|Ziff Davis Media)(?: Co(?:mpany|\.)?)?\]\](?=[\s\.]*[|}])/g, '')
         .replace(/\|\s?publisher\s*\=\s*\[\[(?:Cox|Halifax|North Jersey|Sun-Times|Tampa|Herald|Stephens|WEHCO|\w+) Media(?: Group(?:,? Inc\.)?| Berhad)?\]\](?=[\s\.]*[|}])/g, '')

         .replace(/\| ?publisher ?= ?Star Publications \(Malaysia\)|\[\[Star Publications \(Malaysia\)\]\] ?/gi, '')
         .replace(/\| ?publisher ?= ?(?:New Straits Times Press|\[\[New Straits Times Press\]\]) ?/gi, '')
         .replace(/(\|\s?publisher\s?\=\s?MTV\s?)\|\s*\=\s*\(\w+\)/gi, '$1=') //palliative - correction for unsuppoeted parameters	
		
		//LOCATIONS
        .replace(/( \|location=(?:New York|UK))(\]\])/gi, '$2$1')

         // The following regexes for dab-links are in sets of four. If changing, please ensure all sets are changed
         .replace(/(=[ ]*(?:The ?|)[A-Z]\w*(?: [A-Z]\w*|)) \((South Africa)(?:n newspaper|)\)([ ]*[|}])/g, '$1 | location=$2$3')
         .replace(/(''(?:The ?|)[A-Z]\w*(?: [A-Z]\w*|)) \((South Africa)(?:n newspaper|)\)(''[\.,;])/g, '$1$3$2')
         .replace(/(\[\[((?:The ?|)[A-Z]\w*(?: [A-Z]\w*|)))( \(South Africa(?:n newspaper|)\))(\]\][\.,;]?)/g, '$1$3 | $2$4')
         .replace(/(\|)((?:The ?|)[A-Z]\w*(?: [A-Z]\w*|))(?: \((?:South Africa)(?:n newspaper|)\))(\]\])/g, '$1$2$3')

         .replace(/(=[ ]*)(Billboard|Fast Company|People|Q|Slate|Time Out|Wired) \((?:magazine)\)(?=\s*[|}])/g, '$1$2') //non-standard code
         .replace(/('')(Billboard|Fast Company|People|Q|Slate|Time Out|Wired) \(magazine\)(?='')/g, '$1$2') //non-standard code
         .replace(/(''\[\[(?:Billboard|Fast Company|People|Q|Slate|Time Out|Wired) \(magazine\))(?=\]\]'')/g, '$1|') //non-standard code
         .replace(/(\|)(Billboard|Fast Company|People|Q|Slate|Time Out|Wired)(?: \(magazine\))(?=\]\])/g, '$1$2') 
         .replace(/(=[ ]*)(Quartz) \((?:publication)\)(?=\s*[|}])/g, '$1$2') //non-standard code
         .replace(/('')(Quartz) \(publication\)(?='')/g, '$1$2') //non-standard code
         .replace(/(''\[\[(Quartz) \(publication\))(?=\]\]'')/g, '$1$2') //non-standard code
         .replace(/(\|)(Quartz)(?: \(publication\))(?=\]\])/g, '$1$2') 
         .replace(/(=[ ]*)(Salon) \((?:website)\)(?=\s*[|}])/g, '$1$2') //non-standard code
         .replace(/('')(Salon) \(website\)(?='')/g, '$1$2') //non-standard code
         .replace(/(''\[\[(Salon) \(website\))(?=\]\]'')/g, '$1$2') //non-standard code
         .replace(/(\|)(Salon)(?: \(website\))(?=\]\])/g, '$1$2') 
         
         .replace(/(=[ ]*Daily News) \((New York)\)([ ]*[|}])/g, '$1 | location=$2$3')
         .replace(/(''Daily News) \((New York)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(Daily News)(?: \((New York)\))(\]\])/g, '$1$2$4 | location=$3')
         .replace(/(\[\[)(Daily News)( \((New York)\))(\]\])/g, '$1$2$3 | $2$5 | location=$4')
         .replace(/(=[ ]*Daily Record|=[ ]*Sunday Mail) \((Scotland)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''Daily Record|''Sunday Mail) \((Scotland)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(Daily Record|Sunday Mail)(?: \((Scotland)\))(\]\])/g, '$1$2$4 | location=$3')
         .replace(/(\[\[)(Daily Record|Sunday Mail)( \((Scotland)\))(\]\])/g, '$1$2$3 | $2$5 | location=$4')
         .replace(/(=[ ]*(?:Daily Times)) \((Pakistan)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''(?:Daily Times)) \((Pakistan)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(Daily Times)(?: \(Pakistan\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*Dawn) \((newspaper)\)(\s*[|}])/g, '$1 | location=Pakistan$3')
         .replace(/(''Dawn) \((newspaper)\)('')/g, '$1$3 (Pakistan)')
         .replace(/(\|)(Dawn)(?: \(newspaper\))(\]\])/g, '$1$2$3')

         .replace(/(=[ ]*(?:Daily Star)) \((United Kingdom)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''(?:Daily Star)) \((United Kingdom)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(Daily Star)(?: \(United Kingdom\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*El Mundo) \((Columbia|Spain)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''El Mundo) \((Columbia|Spain)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(El Mundo)(?: \((?:Columbia|Spain)\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The Daily Star) \((Lebanon)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''The Daily Star) \((Lebanon)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The Daily Star)(?: \(Lebanon\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The Gazette) \((Montreal)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''The Gazette) \((Montreal)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The Gazette)(?: \(Montreal\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The Herald) \((Glasgow)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''The Herald) \((Glasgow)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The Herald)(?: \(Glasgow\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*Metro) \((British newspaper)\)(\s*[|}])/g, '$1 | location=UK$3')
         .replace(/(''Metro) \((British newspaper)\)('')/g, '$1$3 (UK)')
         .replace(/(\|)(Metro)(?: \(British newspaper\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The Nation) \((Malawi|Nigeria|Pakistan|Thailand)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''The Nation) \((Malawi|Nigeria|Pakistan|Thailand)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The Nation)(?: \((?:Malawi|Nigeria|Pakistan|Thailand)\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The National) \((Abu Dhabi)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''The National) \((Abu Dhabi)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The National)(?: \(Abu Dhabi\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*La Presse) \((Canadian newspaper)\)(\s*[|}])/g, '$1 | location=Canada$3')
         .replace(/(''La Presse) \((Canadian newspaper)\)('')/g, '$1$3 (Canada)')
         .replace(/(\|)(La Presse)(?: \(Canadian newspaper\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*Les Échos) \((France)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''Les Échos) \((France)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(Les Échos)(?: \(France\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*Panorama) \((Gibraltar)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''Panorama) \((Gibraltar)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(Panorama)(?: \(Gibraltar\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The Pioneer) \((Indian newspaper)\)(\s*[|}])/g, '$1 | location=India$3')
         .replace(/(''The Pioneer) \((Indian newspaper)\)('')/g, '$1$3 (India)')
         .replace(/(\|)(The Pioneer)(?: \(Indian newspaper\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*Sporting Life) \((British newspaper)\)(\s*[|}])/g, '$1 | location=UK$3')
         .replace(/(''Sporting Life) \((British newspaper)\)('')/g, '$1$3 (UK)')
         .replace(/(\|)(Sporting Life)(?: \(British newspaper\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The Standard) \((Hong Kong|Kenya)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''The Standard) \((Hong Kong|Kenya)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The Standard)(?: \((?:Hong Kong|Kenya)\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The Star) \((Malaysia)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''The Star) \((Malaysia)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The Star)(?: \(Malaysia\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The Sun) \((Hong Kong|Malaysia|Nigeria|United Kingdom)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''The Sun) \((Hong Kong|Malaysia|Nigeria|United Kingdom)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The Sun)(?: \((?:Hong Kong|Malaysia|Nigeria|United Kingdom)\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The Telegraph) \(Calcutta\)(?=\s*[|}])/g, '$1 | location=Kolkota')
         .replace(/(''The Telegraph) \((Calcutta)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The Telegraph)(?: \(Calcutta\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*The Daily Telegraph) \(Australia\)(?=\s*[|}])/g, '$1 | location=Australia')
         .replace(/(''The Daily Telegraph) \((Australia)\)('')/g, '$1$3 (Australia)')
         .replace(/(\|)(The Daily Telegraph)(?: \(Australia\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*(?:The Daily Times)) \((Malawi)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''(?:The Daily Times)) \((Malawi)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The Daily Times)(?: \(Malawi\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*(?:The Times)) \((Malta)\)(\s*[|}])/g, '$1 | location=$2$3')
         .replace(/(''(?:The Times)) \((Malta)\)('')/g, '$1$3 ($2)')
         .replace(/(\|)(The Times)(?: \(Malta\))(\]\])/g, '$1$2$3')
         .replace(/(=[ ]*Vogue) \((British magazine)\)(\s*[|}])/g, '$1 | location=UK$3')
         .replace(/(''Vogue) \((British magazine)\)('')/g, '$1$3 (UK)')
         .replace(/(\|)(Vogue)(?: \(British magazine\))(\]\])/g, '$1$2$3')

         .replace(/(= ?(?:ABC|Marca)) \((newspaper)\)([ ]*[|}])/g, '$1 | location=Spain$3')
         .replace(/(''(?:ABC|Marca)) \((newspaper)\)('')/g, '$1$3 (Spain)')
         .replace(/(\|)(ABC|Marca)(?: \(newspaper\))(\]\])/g, '$1$2$3')
         .replace(/(= ?(?:Il Giorno)) \((newspaper)\)([ ]*[|}])/g, '$1 | location=Italy$3')
         .replace(/(''(?:Il Giorno)) \((newspaper)\)('')/g, '$1$3 (Italy)')
         .replace(/(\|)(Il Giorno)(?: \(newspaper\))(\]\])/g, '$1$2$3')
  
         .replace(/(= ?(?:RT)) \((TV network)\)([ ]*[|}])/g, '$1 | location=Russia$3')
         .replace(/(\|)(RT)(?: \(TV network\))(\]\])/g, '$1$2$3')

         .replace(/(=[ ]*\[\[)([^\[\]\(\)\|:]*)( \([^\[\]\(\)\|]*\))(\]\])/gi, '$1$2$3|$2$4')	//adding piping to parentheticals inside parameters

         .replace(/(?:author|agency|publisher)(\s*\=\s*\[\[[^()|]+ \((?:newspaper|magazine)\)\|[^\[\]|]+\]\])/gi, 'work$1')
		
		// removing redundancies
         .replace(/ – (?:Times of India|Rediff.com [\w]*)(?=[ ]?\|)/gi, '')
         .replace(/(?: +[‒–—―] *Times Of India|)(\]. +''The Times of India''\.)indiatimes\.com/gi, '$1')
         .replace(/(?: +[‒–—―] *([\w ]+))(\|\s?(?:publisher|work)\s*\=\s*\1)/gi, '$2')
         .replace(/(\w''\.)indiatimes\.com/gi, '$1')

         .replace(/\|[ ]?language[ ]?\=[ ]?English[ ]*?(?=[|}\n])/gi, '')  //note: adjusted for false positive in infoboxes books
         .replace(/- [\w]*\.com[ ]*\|/gi, '|')
         .replace(/(.) at Discogs(\][\.,;]) Discogs\.com(\.| )/gi, '$1$2Discogs$3')
         .replace(/(\|\s?author\s?\=\s?)(?:posted|publishe[dr]|written)\s?(?:by|on):?\s/gi, '$1')
         .replace(/\|\s?(?:publisher|website|work)(\s?\=MTV)\|\s?publisher\s*\=\s*(?:MTV Networks|Viacom)/gi, '| publisher$1=')

         .replace(/\|\s?(?:publisher|website|work)\s*\=\s*(?:BBC|BBC News(?: Online|))\s*(\|[^}<>]*|)\|\s?publisher\s*\=\s*(?:BBC|BBC News(?: Online|)|British Broadcasting Corporation)(?=[\s\.]*[|}])/g, '| publisher=BBC News $1')
         .replace(/\|\s?(?:publisher|website|work)\s*\=\s*(BBC Sports?)\s*(\|[^}<>]*|)\|\s?publisher\s*\=\s*(?:BBC|BBC News(?: Online|)|British Broadcasting Corporation)(?=[\s\.]*[|}])/g, '| publisher=$1$2')
         .replace(/\|\s?(?:publisher|website|work)\s*\=\s*(Metacritic|\[\[Metacritic\]\])\s*(\|[^}<>]*|)\|\s?publisher\s*\=\s*(CBS Interactive|\[\[CBS Interactive\]\])(?=[\s\.]*[|}])/g, '| publisher=$1$2')

		 //rem duplicated publishers in separate fields (post); rem preceding nbsp
		.replace(/\s?&nbsp;\s?(\|\s?(?:agency|publisher|work)\s*\=\s*)/gi, ' $1')
		.replace(/(?:[‒–—―]+|&#124;)\s*(?:The |)([^\|\}&]{3,})(?:\.com|)\s*(\|\s?(?:agency|publisher|work)\s*\=\s*)\1(?=\s*[|}])/gi, '$2$1')
		.replace(/(?:[‒–—―]+|&#124;)\s*([^\|\}&]{3,})(?:\.com|)\s*(\|\s?(?:agency|publisher|work)\s*\=\s*)(The |)\1(?=\s*[|}])/gi, '$2$3$1')

		/// removing identical/similar entries in 'work' and 'publisher', and in 'work' and 'website'
         .replace(/\|\s?work\s*\=\s*([^=|}\[<>]*)(\|[^}<>]*|)\|\s?publisher\s*\=\s*(?:\1|\[\[\1\]\])\.?(?=\s*[|}])/g, '| work=$1$2') //unlinked work
         .replace(/\|\s?work\s*\=\s*\[\[([^<|\]]*)\]\](\|[^}<>]*|)\|\s?publisher\s*\=\s*(?:\1|\[\[\1\]\])\.?(?=\s*[|}])/g, '| work=$1$2') //unpiped work
         .replace(/\|\s?work\s*\=\s*(\[\[(?:[^<|\]]*)\|([^}<>]*)\]\])(\|[^}<>]*|)\|\s?publisher\s*\=\s*(\1|\2)\.?(?=\s*[|}])/g, '| work=$1$3') //piped work

         .replace(/\|\s?publisher\s*\=\s*([^=|}\[<>]*)(\|[^}<>]*|)\|\s?publisher\s*\=\s*(?:\1|\[\[\1\]\])\.?(?=\s*[|}])/g, '| publisher=$1$2') //unlinked work
         .replace(/\|\s?publisher\s*\=\s*\[\[([^<|\]]*)\]\](\|[^}<>]*|)\|\s?publisher\s*\=\s*(?:\1|\[\[\1\]\])\.?(?=\s*[|}])/g, '| publisher=$1$2') //unpiped work
         .replace(/\|\s?publisher\s*\=\s*(\[\[(?:[^<|\]]*)\|([^}<>]*)\]\])(\|[^}<>]*|)\|\s?publisher\s*\=\s*(\1|\2)\.?(?=\s*[|}])/g, '| publisher=$1$3') //piped work

         .replace(/(\|\s?work\s*\=\s*(?:[^=|}\[<>]*)(?:\|[^}<>]*|))\|\s?website\s*\=\s*(?:[^|}]*)(?=\s*[|}])/g, '$1') //unlinked work
         .replace(/(\|\s?work\s*\=\s*\[\[([^<|\]]*)\]\](?:\|[^}<>]*|))\|\s?website\s*\=\s*(?:[^|}]*)(?=\s*[|}])/g, '$1') //unpiped work
         .replace(/(\|\s?work\s*\=\s*(\[\[(?:[^<|\]]*)\|([^}<>]*)\]\])(?:\|[^}<>]*|))\|\s?website\s*\=\s*(?:[^|}]*)(?=\s*[|}])/g, '$1') //piped work

         .replace(/\|\s?location\s*\=\s*New York(?: City|)\s*(\|[^}<>]*|)\|\s?location\s*\=\s*(New York(?: City|)|USA)(?=[\s\.]*[|}])/g, '| location=New York $1')

         .replace(/(?:\|[ ]*?location=[^\|]*)(\|[ ]*?location=[^|}]*)(?=[|}\n])+/gi, '$1')
         .replace(/(\|[ ]*?publisher=[^=}]*)(?:\|[ ]*?publisher=[^=}]*)(?=[|}\n])+/gi, '$1')
         .replace(/(\|[ ]*?work=[^=}]*)(?:\|[ ]*?work=[^=}]*)(?=[|}\n])+/gi, '$1')

		//remove redundant parameter ('website' is an alias of 'work')
         .replace(/\|\s?work\s*\=\s*([^=|}\[<>]*)(\|[^}<>]*|)\|\s?(?:website|work)\s*\=\s*(?:\[\[[^\]]+\]\]|[|}]+)(?=\s*[|}])/g, '| work=$1$2') //unlinked work
         .replace(/\|\s?work\s*\=\s*\[\[([^<|\]]*)\]\](\|[^}<>]*|)\|\s?(?:website|work)\s*\=\s*(?:\[\[[^\]]+\]\]|[|}]+)(?=\s*[|}])/g, '| work=$1$2') //unpiped work
         .replace(/\|\s?work\s*\=\s*(\[\[(?:[^<|\]]*)\|([^}<>]*)\]\])(\|[^}<>]*|)\|\s?(?:website|work)\s*\=\s*(\1|\2)\.?(?=\s*[|}])/g, '| work=$1$3') //piped work

    	//'work' and its alias (post)
         .replace(/(\|[ ]*?(?:newspaper|work)[ ]*=[^\|}]*(?:\|[^\{\}]*|))(?:\|[ ]*?website[ ]*=[^|}]*)(?=[|}\n])+/gi, '$1')

		//unwinding of unnecessary pipes
         .replace(/\[\[([^\]\|]*)\|\1(?=\]\])/gi, '[[$1')

		// removing artefacts (within citation templates)
         .replace(/(?:&#124; (?:Daily|English|(?:Mail |)Online|Music|News|Indian Express))(?=\s*\|)+/gi, '')

         .replace(/(?:(?:[ ]+&#124;[ ]+HighBeam Research[ ]+[-–][ ]+FREE trial[ ]+|)\|publisher=Highbeam.com)/gi, '')
         .replace(/( &#124; [\w, ]*?)(?=[ ]&#124)/gi, '')
         .replace(/( &#124; Comment is free)/gi, '')
         .replace(/\|\s?title\s*\=\s*BBC (?:News|Sport)\s?(?:[-–]|&#124; )\s?/gi, '|title=')
         .replace(/\|\s?title\s*\=\s*Asia Times Online\s?(?:[-–]|&#124; |:+)\s?/gi, '|title=')
         .replace(/(?:Entertainment|Football|(?:inter|)national|Latest|local|UK|world|) News &#124;(?=[ ]?[&\|])/gi, '')
         .replace(/(?:[-–|]|&#124;)[ ]*(?:Entertainment|Football|(?:inter|)national|Latest|local|UK|world|) News(?=[ ]?[&\|])/gi, '')
         .replace(/<!-- Bot generated title -->/gi, '')
         .replace(/(\|\s?title\s*\=\s*[^|}]+?)[-‒–—―] *([^|}]+?)(\|\s?(?:publisher|work)\s*\=\s*\2)/gi, '$1$3')
         .replace(/(\|\s?title\s*\=\s*([^|}]+?))[-‒–—―] *([^|}]+?\|\s?(?:publisher|work)\s*\=\s*\2)/gi, '$1$3')

         .replace(/(DOC|PDF)\) \./gi, '$1).')

		//MINE
		//Remove ", PhD" and "Ph. D." and all variations
		.replace(/(last|first|author)(\d*)\s*=(.+?),*\s*ph\.*\s*d\.*/gi, '$1$2=$3');
		
		//LOOPING THROUGH REFS
		//Make spaces around pipes nice
		var originalRefs = editor.get().match(/<ref((.|\n)+?)(?=[\/|ref]>)/g);
		for (var i = 0; i < originalRefs.length; i++) {
  			var thisRef = originalRefs[i];
			var goodPipes = thisRef.replace(/\n\s*\|/gi, ' |')
						       .replace(/\|\s*\n/gi, '| ')
		  					   .replace(/(\S)\|(?!.+\]\])/gi, '$1 |')
                    			.replace(/\|(\S)(?!.+\]\])/gi, '| $1');
			thisRef = goodPipes;

			// Fix vauthors and authors and such
    //  		var vauthors = thisRef.match(/v*authors\s*=\s*(.+?)\|/gi);
			 
			 //if (vauthors) {
    //       		var newAuthors = '';
    //             var vauthor = vauthors[0].split(/=|,|;/);

    //             for (var j = 0; j < vauthor.length; j++) {
    //             	if ( j !== 0 ) {
    //             		vauthor = vauthor[j].replace('|', '').trim();
    //                 	newAuthors = newAuthors+'last'+ j +'=' + vauthor +' | ';
    //             	}
    //              }
    //     		thisRef = thisRef.replace(vauthors,newAuthors);
			 //}
			 
			 //Fix author reading as "First Last" or First M. Last
			 var authors = thisRef.match(/author(\d*)\s*=\s*(.[^,]*?)\|/gi);
			 if (authors) { for (var k = 0; k < authors.length; k++) {

			 	 var author = authors[k];

				 if (author) { //got more than just a number
					var authorNumber = author.match(/\d/);
						if (authorNumber == null) {authorNumber = ''}
						else {authorNumber = authorNumber[0];}

				 	var cleanAuthor = author.replace(/author\d*\s*=\s*/gi, '')
				 					.replace(/\s*\|/gi, '')
				 				    .trim();

	 			    var authorNameArray = cleanAuthor.split(' ');

	 		    	//if middle one is "van" or "de", stick it with last. Otherwise, stick it with first.
	 		    	if (authorNameArray.length == 3) { //And if they have more than 3, they can go fuck themselves
	  			    			var	authorFirst		= authorNameArray[0],
	  			    			 	authorMiddle 	= authorNameArray[1],
	 			    				authorLast		= authorNameArray[2];
	 			    				
	 			    		if (authorMiddle == 'van' || authorMiddle == 'de') {
	 			    			thisRef = thisRef.replace(author, 'last' +authorNumber+ '=' +authorMiddle+ ' ' +authorLast+ 
	 			    											  ' | first' +authorNumber+ '=' +authorFirst+ ' | ');
	 			    		}
	 			    		else {
	 			    			thisRef = thisRef.replace(author, 'last' +authorNumber+ '=' +authorLast+ 
	 			    										       ' | first' +authorNumber+ '=' +authorFirst+ ' ' +authorMiddle+ ' | ');
	 			    		}
	 			    }
	 			    if (authorNameArray.length == 2) {
	      				var	authorFirst	= authorNameArray[0],
	      				    authorLast  = authorNameArray[1];
	      				    
	      				    thisRef = thisRef.replace(author, 'last' +authorNumber+ '=' +authorLast+ 
		 			    								' | first' +authorNumber+ '=' +authorFirst+ ' | ');
	 			    }
				}
			 }}
 			 //Change "author=Last, First" to use last= and first=
			 var lastFirstAuthors = thisRef.match(/author(\d*)\s*=\s*(.*?),\s*(.*?)\s*\|/gi);

			 if (lastFirstAuthors) { for (var m = 0; m < lastFirstAuthors.length; m++) {
				 
				 var lastFirstAuthor = lastFirstAuthors[m];
					 var authorNumber = lastFirstAuthor.match(/\d/);
						if (authorNumber == null) {authorNumber = ''}
						else {authorNumber = authorNumber[0];}
						
					 var lastFirstAuthorArray = lastFirstAuthor.replace(/author\d*\s*=\s*/gi, '')
										 						.replace(/\s*\|/gi, '')
										 				    	.trim()
										 				    	.split(',');
					 
				 if (lastFirstAuthorArray) { 
					
					var authorLast   = lastFirstAuthorArray[0].replace('|', '').trim(),
						authorFirst  = lastFirstAuthorArray[1].replace('|', '').trim();
				 
 			    thisRef = thisRef.replace(lastFirstAuthor, 'last' +authorNumber+ '=' +authorLast+ 
 			    										' | first' +authorNumber+ '=' +authorFirst+ ' | ' );
	 			    
				 }
			 }}
			 
			 //Remove accessdate from certain citation types

			 				 
			 var citationType = thisRef.match(/{{cite ([a-z]*)/i),
				 accessdate = thisRef.match(/(access\-*date\s*=.*?)[\|}]/i);

			if (accessdate && citationType) {
				citationType = citationType[1];
				accessdate = accessdate[1];

				 if (citationType == 'journal' || citationType == 'book') {
				 	thisRef = thisRef.replace(accessdate, '');
				 } 
			}
			 
			 //Fix google books links
			 var uglyBooksLink = thisRef.match(/books\.google\.com\/books\?id=([a-z|0-9]+).*&pg=([a-z|0-9]+)&.*?[\s\|}]/i);
			 var googleID, googlePage;
			 if (uglyBooksLink) {
				    googleID = uglyBooksLink[1],
					googlePage = uglyBooksLink[2];
			 }
			 console.log(uglyBooksLink);
			 if (uglyBooksLink && googleID && googlePage) {
			 	thisRef = thisRef.replace(uglyBooksLink[0], 'books.google.com/books?id=' +googleID+ '&pg=' +googlePage);
			 }
			 
			 
			 
			 editor.replace(originalRefs[i], thisRef);
 			} // END LOOPING THROUGH REFS
  	
		editor.clickDiff();
		}
	}

]);
});
// </nowiki>