Jump to content

Module:MultiReplace

Permanently protected module
From Wikipedia, the free encyclopedia

localp={}localfunctionMultiReplace(args)localinput=args[1]or"{{{1}}}"localplain=args.plain=="yes"locali=1localchangeList={}whileargs[i*2]dolocalchange={pattern=args[i*2],repl=args[i*2+1]}ifnotchange.replthenreturnrequire('Module:Error').error{'MultiReplace: Unpaired argument: <code>'..(i*2)..' = '..mw.text.nowiki(change.pattern)..'</code>'}endchangeList[i]=changei=i+1endlocalmatchList={}localpos=1locallen=mw.ustring.len(input)localresult=""whilepos<=lendolocalbestStart=len+1localbestStop=lenlocalbestChangefor_,changeinipairs(changeList)dolocalstart,stop=mw.ustring.find(input,change.pattern,pos,plain)ifstartand(start<bestStart)thenbestStart=startbestStop=stopbestChange=changeendendresult=result..mw.ustring.sub(input,pos,bestStart-1)ifbestChangethenlocalfragment=mw.ustring.sub(input,bestStart,bestStop)result=result..(plainandbestChange.replormw.ustring.gsub(fragment,bestChange.pattern,bestChange.repl,1))endpos=bestStop+1endreturnresultendfunctionp.main(frame,...)localargs=type(frame)~='table'and{frame,...}ortype(frame.args)~='table'andframeorframe.args[1]andframe.argsorframe:getParent().argsreturnMultiReplace(args)endreturnp
close