Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Module:MultiReplace

Permanently protected module
From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
WarningThis Lua module is used onapproximately 1,930,000 pages, or roughly 3% of all pages.
To avoid major disruption and server load, any changes should be tested in the module's/sandbox or/testcases subpages, or in your ownmodule sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on thetalk page before implementing them.
This module depends on the following other modules:
For a function to search for multiple patterns, see {{#invoke:string2|matchAny}}. For a function to replace multiple parameters at once, see {{#invoke:params|mapping_by_replacing}}.

Replaces matches of multiple patterns in a given string with given replacements. For each replacement instance, the pattern matchingat the lowest position is chosen. If there are multiple such patterns, then the one specified earliest in the pattern list is chosen.

Usage

{{#invoke:MultiReplace|main|input|plain=yes (optional)|pattern1|replacement1|pattern2|replacement2... }}

Ifplain=yes is specified, then the patterns and replacements are treated as plain text, otherwise asLua Unicode patterns.

An equals sign in a pattern will trigger anUnpaired argument error. Use{{=}}, which expands to an equals sign that will not be interpreted.

The abovedocumentation istranscluded fromModule:MultiReplace/doc.(edit |history)
Editors can experiment in this module'ssandbox(edit |diff) and testcases(create) pages.
Subpages of this module.

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
Retrieved from "https://en.wikipedia.org/w/index.php?title=Module:MultiReplace&oldid=1122097663"
Category:
Hidden category:

[8]ページ先頭

©2009-2025 Movatter.jp