Pergi ke kandungan

Modul:MultiReplace

Daripada Wikipedia, ensiklopedia bebas.

Pendokumenan untuk modul ini boleh diciptakan diModul:MultiReplace/doc

p={}

localfunctionMultiReplace(args)
localinput=args[1]or"{{{1}}}"
localplain=args.plain=="yes"

locali=1
localchangeList={}
whileargs[i*2]do
localchange={pattern=args[i*2],repl=args[i*2+1]}
ifnotchange.replthen
returnrequire('Module:Error').error{
'MultiReplace: Unpaired argument: <code>'..(i*2)..' = '..change.pattern..'</code>'
}
end
changeList[i]=change
i=i+1
end

localmatchList={}
localpos=1
locallen=mw.ustring.len(input)
localresult=""
whilepos<=lendo
localbestStart=len+1
localbestStop=len
localbestChange
for_,changeinipairs(changeList)do
localstart,stop=mw.ustring.find(input,change.pattern,pos,plain)
ifstartand(start<bestStart)then
bestStart=start
bestStop=stop
bestChange=change
end
end
result=result..mw.ustring.sub(input,pos,bestStart-1)
ifbestChangethen
localfragment=mw.ustring.sub(input,bestStart,bestStop)
result=result..(plainandbestChange.replor
mw.ustring.gsub(fragment,bestChange.pattern,bestChange.repl,1))
end
pos=bestStop+1
end
returnresult
end

functionp.main(frame,...)
localargs=
type(frame)~='table'and{frame,...}or
type(frame.args)~='table'andframeor
frame.args[1]andframe.argsor
frame:getParent().args
returnMultiReplace(args)
end

returnp