Title: Module:String - freem URL Source: https://freemwiki.com/index.php?oldid=579572&title=Module%3AString Markdown Content: [Jump to content](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#bodyContent) - [x] Main menu Main menu move to sidebar hide Navigation * [Main page](https://freemwiki.com/wiki/Main_Page "Visit the main page [alt-shift-z]") * [Recent changes](https://freemwiki.com/wiki/Special:RecentChanges "A list of recent changes in the wiki [alt-shift-r]") * [Random page](https://freemwiki.com/wiki/Special:Random "Load a random page [alt-shift-x]") * [Help about MediaWiki](https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Contents) Legal * [Terms](https://freemwiki.com/wiki/Terms_of_Service) * [Privacy Policy](https://freemwiki.com/wiki/Privacy_Policy) * [About](https://freemwiki.com/wiki/About) * [Contact](https://freemwiki.com/wiki/Contact) [![Image 1](https://freemwiki.com/images/freem.svg)**freem**](https://freemwiki.com/wiki/Main_Page) [Search](https://freemwiki.com/wiki/Special:Search "Search freem [alt-shift-f]") Search - [x] Appearance * [Create account](https://freemwiki.com/index.php?title=Special:CreateAccount&returnto=Module%3AString&returntoquery=oldid%3D579572 "You are encouraged to create an account and log in; however, it is not mandatory") * [Log in](https://freemwiki.com/index.php?title=Special:UserLogin&returnto=Module%3AString&returntoquery=oldid%3D579572 "You are encouraged to log in; however, it is not mandatory [alt-shift-o]") - [x] Personal tools * [Create account](https://freemwiki.com/index.php?title=Special:CreateAccount&returnto=Module%3AString&returntoquery=oldid%3D579572 "You are encouraged to create an account and log in; however, it is not mandatory") * [Log in](https://freemwiki.com/index.php?title=Special:UserLogin&returnto=Module%3AString&returntoquery=oldid%3D579572 "You are encouraged to log in; however, it is not mandatory [alt-shift-o]") Pages for logged out editors [learn more](https://freemwiki.com/wiki/Help:Introduction) * [Contributions](https://freemwiki.com/wiki/Special:MyContributions "A list of edits made from this IP address [alt-shift-y]") * [Talk](https://freemwiki.com/wiki/Special:MyTalk "Discussion about edits from this IP address [alt-shift-n]") # Module:String - [x] Add languages * [Module](https://freemwiki.com/wiki/Module:String "View the module page [alt-shift-c]") * [Discussion](https://freemwiki.com/index.php?title=Module_talk:String&action=edit&redlink=1 "Discussion about the content page (page does not exist) [alt-shift-t]") - [x] English * [Read](https://freemwiki.com/wiki/Module:String) * [Edit source](https://freemwiki.com/index.php?title=Module:String&action=edit "Edit the source code of this page [alt-shift-e]") * [View history](https://freemwiki.com/index.php?title=Module:String&action=history "Past revisions of this page [alt-shift-h]") - [x] Tools Tools move to sidebar hide Actions * [Read](https://freemwiki.com/wiki/Module:String) * [Edit source](https://freemwiki.com/index.php?title=Module:String&action=edit "Edit the source code of this page [alt-shift-e]") * [View history](https://freemwiki.com/index.php?title=Module:String&action=history) General * [What links here](https://freemwiki.com/wiki/Special:WhatLinksHere/Module:String "A list of all wiki pages that link here [alt-shift-j]") * [Related changes](https://freemwiki.com/wiki/Special:RecentChangesLinked/Module:String "Recent changes in pages linked from this page [alt-shift-k]") * [Special pages](https://freemwiki.com/wiki/Special:SpecialPages "A list of all special pages [alt-shift-q]") * [Printable version](javascript:print(); "Printable version of this page [alt-shift-p]") * [Permanent link](https://freemwiki.com/index.php?title=Module:String&oldid=579572 "Permanent link to this revision of this page") * [Page information](https://freemwiki.com/index.php?title=Module:String&action=info "More information about this page") Appearance move to sidebar hide From freem Revision as of 06:18, 21 October 2025 by [Lukegao](https://freemwiki.com/index.php?title=User:Lukegao&action=edit&redlink=1 "User:Lukegao (page does not exist)")([talk](https://freemwiki.com/index.php?title=User_talk:Lukegao&action=edit&redlink=1 "User talk:Lukegao (page does not exist)") | [contribs](https://freemwiki.com/wiki/Special:Contributions/Lukegao "Special:Contributions/Lukegao"))(1 revision imported) ([diff](https://freemwiki.com/index.php?title=Module:String&diff=prev&oldid=579572 "Module:String")) [← Older revision](https://freemwiki.com/index.php?title=Module:String&direction=prev&oldid=579572 "Module:String") | Latest revision (diff) | Newer revision → (diff) _Documentation for this module may be created at [Module:String/doc](https://freemwiki.com/index.php?title=Module:String/doc&action=edit&redlink=1 "Module:String/doc (page does not exist)")_ [](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-1)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-2)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-3)This module is intended to provide access to basic string functions.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-4)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-5)Most of the functions provided here can be invoked with named parameters,[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-6)unnamed parameters, or a mixture. If named parameters are used, Mediawiki will[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-7)automatically remove any leading or trailing whitespace from the parameter.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-8)Depending on the intended use, it may be advantageous to either preserve or[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-9)remove such whitespace.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-10)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-11)Global options[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-12) ignore_errors: If set to 'true' or 1, any error condition will result in[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-13) an empty string being returned rather than an error message.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-14)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-15) error_category: If an error occurs, specifies the name of a category to[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-16) include with the error message. The default category is[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-17) [Category:Errors reported by Module String].[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-18)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-19) no_category: If set to 'true' or 1, no category will be added if an error[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-20) is generated.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-21)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-22)Unit tests for this module are available at Module:String/tests.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-23)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-24)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-25)local str = {}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-26)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-27)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-28)len[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-29)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-30)This function returns the length of the target string.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-31)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-32)Usage:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-33){{#invoke:String|len|target_string|}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-34)OR[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-35){{#invoke:String|len|s=target_string}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-36)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-37)Parameters[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-38) s: The string whose length to report[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-39)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-40)If invoked using named parameters, Mediawiki will automatically remove any leading or[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-41)trailing whitespace from the target string.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-42)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-43)function str.len( frame )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-44)local new_args = str._getParameters( frame.args, {'s'} )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-45)local s = new_args['s'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-46)return mw.ustring.len( s )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-47)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-48)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-49)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-50)sub[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-51)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-52)This function returns a substring of the target string at specified indices.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-53)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-54)Usage:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-55){{#invoke:String|sub|target_string|start_index|end_index}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-56)OR[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-57){{#invoke:String|sub|s=target_string|i=start_index|j=end_index}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-58)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-59)Parameters[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-60) s: The string to return a subset of[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-61) i: The first index of the substring to return, defaults to 1.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-62) j: The last index of the string to return, defaults to the last character.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-63)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-64)The first character of the string is assigned an index of 1. If either i or j[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-65)is a negative value, it is interpreted the same as selecting a character by[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-66)counting from the end of the string. Hence, a value of -1 is the same as[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-67)selecting the last character of the string.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-68)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-69)If the requested indices are out of range for the given string, an error is[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-70)reported.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-71)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-72)function str.sub( frame )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-73)local new_args = str._getParameters( frame.args, { 's', 'i', 'j' } )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-74)local s = new_args['s'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-75)local i = tonumber( new_args['i'] ) or 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-76)local j = tonumber( new_args['j'] ) or -1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-77)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-78)local len = mw.ustring.len( s )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-79)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-80)-- Convert negatives for range checking[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-81)if i < 0 then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-82)i = len + i + 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-83)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-84)if j < 0 then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-85)j = len + j + 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-86)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-87)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-88)if i > len or j > len or i < 1 or j < 1 then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-89)return str._error( 'String subset index out of range' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-90)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-91)if j < i then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-92)return str._error( 'String subset indices out of order' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-93)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-94)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-95)return mw.ustring.sub( s, i, j )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-96)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-97)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-98)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-99)This function implements that features of {{[str sub old](https://freemwiki.com/wiki/Template:Str_sub_old "Template:Str sub old")}} and is kept in order[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-100)to maintain these older templates.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-101)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-102)function str.sublength( frame )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-103)local i = tonumber( frame.args.i ) or 0[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-104)local len = tonumber( frame.args.len )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-105)return mw.ustring.sub( frame.args.s, i + 1, len and ( i + len ) )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-106)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-107)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-108)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-109)_match[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-110)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-111)This function returns a substring from the source string that matches a[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-112)specified pattern. It is exported for use in other modules[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-113)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-114)Usage:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-115)strmatch = require("Module:String")._match[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-116)sresult = strmatch( s, pattern, start, match, plain, nomatch )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-117)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-118)Parameters[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-119) s: The string to search[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-120) pattern: The pattern or string to find within the string[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-121) start: The index within the source string to start the search. The first[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-122) character of the string has index 1. Defaults to 1.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-123) match: In some cases it may be possible to make multiple matches on a single[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-124) string. This specifies which match to return, where the first match is[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-125) match= 1. If a negative number is specified then a match is returned[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-126) counting from the last match. Hence match = -1 is the same as requesting[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-127) the last match. Defaults to 1.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-128) plain: A flag indicating that the pattern should be understood as plain[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-129) text. Defaults to false.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-130) nomatch: If no match is found, output the "nomatch" value rather than an error.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-131)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-132)For information on constructing Lua patterns, a form of [regular expression], see:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-133)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-134)* [http://www.lua.org/manual/5.1/manual.html#5.4.1](http://www.lua.org/manual/5.1/manual.html#5.4.1)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-135)* [http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns](http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-136)* [http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns](http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-137)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-138)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-139)-- This sub-routine is exported for use in other modules[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-140)function str._match( s, pattern, start, match_index, plain_flag, nomatch )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-141)if s == '' then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-142)return str._error( 'Target string is empty' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-143)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-144)if pattern == '' then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-145)return str._error( 'Pattern string is empty' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-146)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-147)start = tonumber(start) or 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-148)if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-149)return str._error( 'Requested start is out of range' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-150)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-151)if match_index == 0 then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-152)return str._error( 'Match index is out of range' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-153)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-154)if plain_flag then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-155)pattern = str._escapePattern( pattern )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-156)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-157)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-158)local result[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-159)if match_index == 1 then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-160)-- Find first match is simple case[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-161)result = mw.ustring.match( s, pattern, start )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-162)else[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-163)if start > 1 then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-164)s = mw.ustring.sub( s, start )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-165)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-166)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-167)local iterator = mw.ustring.gmatch(s, pattern)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-168)if match_index > 0 then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-169)-- Forward search[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-170)for w in iterator do[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-171)match_index = match_index - 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-172)if match_index == 0 then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-173)result = w[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-174)break[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-175)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-176)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-177)else[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-178)-- Reverse search[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-179)local result_table = {}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-180)local count = 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-181)for w in iterator do[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-182)result_table[count] = w[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-183)count = count + 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-184)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-185)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-186)result = result_table[ count + match_index ][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-187)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-188)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-189)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-190)if result == nil then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-191)if nomatch == nil then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-192)return str._error( 'Match not found' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-193)else[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-194)return nomatch[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-195)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-196)else[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-197)return result[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-198)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-199)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-200)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-201)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-202)match[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-203)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-204)This function returns a substring from the source string that matches a[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-205)specified pattern.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-206)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-207)Usage:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-208){{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-209)OR[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-210){{#invoke:String|match|s=source_string|pattern=pattern_string|start=start_index[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-211) |match=match_number|plain=plain_flag|nomatch=nomatch_output}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-212)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-213)Parameters[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-214) s: The string to search[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-215) pattern: The pattern or string to find within the string[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-216) start: The index within the source string to start the search. The first[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-217) character of the string has index 1. Defaults to 1.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-218) match: In some cases it may be possible to make multiple matches on a single[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-219) string. This specifies which match to return, where the first match is[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-220) match= 1. If a negative number is specified then a match is returned[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-221) counting from the last match. Hence match = -1 is the same as requesting[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-222) the last match. Defaults to 1.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-223) plain: A flag indicating that the pattern should be understood as plain[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-224) text. Defaults to false.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-225) nomatch: If no match is found, output the "nomatch" value rather than an error.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-226)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-227)If invoked using named parameters, Mediawiki will automatically remove any leading or[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-228)trailing whitespace from each string. In some circumstances this is desirable, in[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-229)other cases one may want to preserve the whitespace.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-230)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-231)If the match_number or start_index are out of range for the string being queried, then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-232)this function generates an error. An error is also generated if no match is found.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-233)If one adds the parameter ignore_errors=true, then the error will be suppressed and[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-234)an empty string will be returned on any failure.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-235)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-236)For information on constructing Lua patterns, a form of [regular expression], see:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-237)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-238)* [http://www.lua.org/manual/5.1/manual.html#5.4.1](http://www.lua.org/manual/5.1/manual.html#5.4.1)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-239)* [http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns](http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-240)* [http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns](http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-241)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-242)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-243)-- This is the entry point for #invoke:String|match[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-244)function str.match( frame )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-245)local new_args = str._getParameters( frame.args, {'s', 'pattern', 'start', 'match', 'plain', 'nomatch'} )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-246)local s = new_args['s'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-247)local start = tonumber( new_args['start'] ) or 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-248)local plain_flag = str._getBoolean( new_args['plain'] or false )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-249)local pattern = new_args['pattern'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-250)local match_index = math.floor( tonumber(new_args['match']) or 1 )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-251)local nomatch = new_args['nomatch'][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-252)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-253)return str._match( s, pattern, start, match_index, plain_flag, nomatch )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-254)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-255)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-256)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-257)pos[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-258)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-259)This function returns a single character from the target string at position pos.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-260)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-261)Usage:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-262){{#invoke:String|pos|target_string|index_value}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-263)OR[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-264){{#invoke:String|pos|target=target_string|pos=index_value}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-265)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-266)Parameters[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-267) target: The string to search[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-268) pos: The index for the character to return[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-269)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-270)If invoked using named parameters, Mediawiki will automatically remove any leading or[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-271)trailing whitespace from the target string. In some circumstances this is desirable, in[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-272)other cases one may want to preserve the whitespace.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-273)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-274)The first character has an index value of 1.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-275)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-276)If one requests a negative value, this function will select a character by counting backwards[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-277)from the end of the string. In other words pos = -1 is the same as asking for the last character.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-278)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-279)A requested value of zero, or a value greater than the length of the string returns an error.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-280)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-281)function str.pos( frame )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-282)local new_args = str._getParameters( frame.args, {'target', 'pos'} )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-283)local target_str = new_args['target'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-284)local pos = tonumber( new_args['pos'] ) or 0[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-285)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-286)if pos == 0 or math.abs(pos) > mw.ustring.len( target_str ) then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-287)return str._error( 'String index out of range' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-288)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-289)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-290)return mw.ustring.sub( target_str, pos, pos )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-291)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-292)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-293)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-294)str_find[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-295)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-296)This function duplicates the behavior of {{[str_find](https://freemwiki.com/wiki/Template:Str_find "Template:Str find")}}, including all of its quirks.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-297)This is provided in order to support existing templates, but is NOT RECOMMENDED for[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-298)new code and templates. New code is recommended to use the "find" function instead.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-299)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-300)Returns the first index in "source" that is a match to "target". Indexing is 1-based,[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-301)and the function returns -1 if the "target" string is not present in "source".[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-302)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-303)Important Note: If the "target" string is empty / missing, this function returns a[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-304)value of "1", which is generally unexpected behavior, and must be accounted for[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-305)separatetly.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-306)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-307)function str.str_find( frame )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-308)local new_args = str._getParameters( frame.args, {'source', 'target'} )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-309)local source_str = new_args['source'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-310)local target_str = new_args['target'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-311)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-312)if target_str == '' then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-313)return 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-314)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-315)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-316)local start = mw.ustring.find( source_str, target_str, 1, true )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-317)if start == nil then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-318)start = -1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-319)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-320)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-321)return start[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-322)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-323)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-324)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-325)find[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-326)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-327)This function allows one to search for a target string or pattern within another[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-328)string.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-329)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-330)Usage:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-331){{#invoke:String|find|source_str|target_string|start_index|plain_flag}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-332)OR[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-333){{#invoke:String|find|source=source_str|target=target_str|start=start_index|plain=plain_flag}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-334)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-335)Parameters[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-336) source: The string to search[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-337) target: The string or pattern to find within source[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-338) start: The index within the source string to start the search, defaults to 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-339) plain: Boolean flag indicating that target should be understood as plain[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-340) text and not as a Lua style regular expression, defaults to true[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-341)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-342)If invoked using named parameters, Mediawiki will automatically remove any leading or[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-343)trailing whitespace from the parameter. In some circumstances this is desirable, in[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-344)other cases one may want to preserve the whitespace.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-345)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-346)This function returns the first index >= "start" where "target" can be found[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-347)within "source". Indices are 1-based. If "target" is not found, then this[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-348)function returns 0. If either "source" or "target" are missing / empty, this[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-349)function also returns 0.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-350)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-351)This function should be safe for UTF-8 strings.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-352)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-353)function str.find( frame )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-354)local new_args = str._getParameters( frame.args, {'source', 'target', 'start', 'plain' } )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-355)local source_str = new_args['source'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-356)local pattern = new_args['target'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-357)local start_pos = tonumber(new_args['start']) or 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-358)local plain = new_args['plain'] or true[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-359)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-360)if source_str == '' or pattern == '' then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-361)return 0[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-362)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-363)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-364)plain = str._getBoolean( plain )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-365)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-366)local start = mw.ustring.find( source_str, pattern, start_pos, plain )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-367)if start == nil then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-368)start = 0[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-369)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-370)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-371)return start[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-372)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-373)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-374)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-375)replace[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-376)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-377)This function allows one to replace a target string or pattern within another[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-378)string.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-379)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-380)Usage:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-381){{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-382)OR[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-383){{#invoke:String|replace|source=source_string|pattern=pattern_string|replace=replace_string|[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-384) count=replacement_count|plain=plain_flag}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-385)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-386)Parameters[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-387) source: The string to search[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-388) pattern: The string or pattern to find within source[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-389) replace: The replacement text[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-390) count: The number of occurences to replace, defaults to all.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-391) plain: Boolean flag indicating that pattern should be understood as plain[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-392) text and not as a Lua style regular expression, defaults to true[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-393)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-394)function str.replace( frame )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-395)local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'count', 'plain' } )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-396)local source_str = new_args['source'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-397)local pattern = new_args['pattern'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-398)local replace = new_args['replace'] or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-399)local count = tonumber( new_args['count'] )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-400)local plain = new_args['plain'] or true[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-401)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-402)if source_str == '' or pattern == '' then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-403)return source_str[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-404)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-405)plain = str._getBoolean( plain )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-406)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-407)if plain then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-408)pattern = str._escapePattern( pattern )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-409)replace = string.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-410)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-411)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-412)local result[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-413)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-414)if count ~= nil then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-415)result = mw.ustring.gsub( source_str, pattern, replace, count )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-416)else[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-417)result = mw.ustring.gsub( source_str, pattern, replace )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-418)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-419)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-420)return result[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-421)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-422)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-423)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-424) simple function to pipe string.rep to templates.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-425)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-426)function str.rep( frame )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-427)local repetitions = tonumber( frame.args[2] )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-428)if not repetitions then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-429)return str._error( 'function rep expects a number as second parameter, received "' .. ( frame.args[2] or '' ) .. '"' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-430)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-431)return string.rep( frame.args[1] or '', repetitions )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-432)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-433)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-434)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-435)escapePattern[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-436)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-437)This function escapes special characters from a Lua string pattern. See [1][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-438)for details on how patterns work.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-439)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-440)[1] [https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns](https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-441)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-442)Usage:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-443){{#invoke:String|escapePattern|pattern_string}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-444)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-445)Parameters[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-446) pattern_string: The pattern string to escape.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-447)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-448)function str.escapePattern( frame )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-449)local pattern_str = frame.args[1][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-450)if not pattern_str then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-451)return str._error( 'No pattern string specified' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-452)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-453)local result = str._escapePattern( pattern_str )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-454)return result[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-455)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-456)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-457)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-458)count[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-459)This function counts the number of occurrences of one string in another.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-460)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-461)function str.count(frame)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-462)local args = str._getParameters(frame.args, {'source', 'pattern', 'plain'})[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-463)local source = args.source or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-464)local pattern = args.pattern or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-465)local plain = str._getBoolean(args.plain or true)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-466)if plain then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-467)pattern = str._escapePattern(pattern)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-468)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-469)local _, count = mw.ustring.gsub(source, pattern, '')[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-470)return count[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-471)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-472)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-473)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-474)endswith[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-475)This function determines whether a string ends with another string.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-476)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-477)function str.endswith(frame)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-478)local args = str._getParameters(frame.args, {'source', 'pattern'})[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-479)local source = args.source or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-480)local pattern = args.pattern or ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-481)if pattern == '' then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-482)-- All strings end with the empty string.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-483)return "yes"[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-484)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-485)if mw.ustring.sub(source, -mw.ustring.len(pattern), -1) == pattern then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-486)return "yes"[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-487)else[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-488)return ""[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-489)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-490)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-491)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-492)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-493)join[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-494)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-495)Join all non empty arguments together; the first argument is the separator.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-496)Usage:[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-497){{#invoke:String|join|sep|one|two|three}}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-498)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-499)function str.join(frame)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-500)local args = {}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-501)local sep[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-502)for _, v in ipairs( frame.args ) do[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-503)if sep then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-504)if v ~= '' then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-505)table.insert(args, v)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-506)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-507)else[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-508)sep = v[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-509)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-510)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-511)return table.concat( args, sep or '' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-512)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-513)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-514)-- findpagetext returns the position of a piece of text in a page[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-515)-- First positional parameter or |text is the search text[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-516)-- Optional parameter |title is the page title, defaults to current page[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-517)-- Optional parameter |plain is either true for plain search (default) or false for Lua pattern search[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-518)-- Optional parameter |nomatch is the return value when no match is found; default is nil[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-519)function str._findpagetext(args)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-520)-- process parameters[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-521)local nomatch = args.nomatch or ""[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-522)if nomatch == "" then nomatch = nil end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-523)--[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-524)local text = mw.text.trim(args[1] or args.text or "")[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-525)if text == "" then return nil end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-526)--[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-527)local title = args.title or ""[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-528)local titleobj[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-529)if title == "" then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-530)titleobj = mw.title.getCurrentTitle()[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-531)else[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-532)titleobj = mw.title.new(title)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-533)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-534)--[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-535)local plain = args.plain or ""[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-536)if plain:sub(1, 1) == "f" then plain = false else plain = true end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-537)-- get the page content and look for 'text' - return position or nomatch[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-538)local content = titleobj and titleobj:getContent()[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-539)return content and mw.ustring.find(content, text, 1, plain) or nomatch[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-540)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-541)function str.findpagetext(frame)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-542)local args = frame.args[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-543)local pargs = frame:getParent().args[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-544)for k, v in pairs(pargs) do[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-545)args[k] = v[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-546)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-547)if not (args[1] or args.text) then return nil end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-548)-- just the first value[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-549)return (str._findpagetext(args))[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-550)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-551)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-552)Helper function that populates the argument list given that user may need to use a mix of[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-553)named and unnamed parameters. This is relevant because named parameters are not[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-554)identical to unnamed parameters due to string trimming, and when dealing with strings[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-555)we sometimes want to either preserve or remove that whitespace depending on the application.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-556)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-557)function str._getParameters( frame_args, arg_list )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-558)local new_args = {}[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-559)local index = 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-560)local value[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-561)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-562)for _, arg in ipairs( arg_list ) do[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-563)value = frame_args[arg][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-564)if value == nil then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-565)value = frame_args[index][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-566)index = index + 1[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-567)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-568)new_args[arg] = value[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-569)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-570)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-571)return new_args[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-572)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-573)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-574)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-575)Helper function to handle error messages.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-576)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-577)function str._error( error_str )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-578)local frame = mw.getCurrentFrame()[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-579)local error_category = frame.args.error_category or 'Errors reported by Module String'[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-580)local ignore_errors = frame.args.ignore_errors or false[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-581)local no_category = frame.args.no_category or false[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-582)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-583)if str._getBoolean(ignore_errors) then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-584)return ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-585)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-586)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-587)local error_str = 'String Module Error: ' .. error_str .. ''[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-588)if error_category ~= '' and not str._getBoolean( no_category ) then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-589)error_str = '[[Category:' .. error_category .. ']]' .. error_str[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-590)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-591)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-592)return error_str[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-593)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-594)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-595)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-596)Helper Function to interpret boolean strings[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-597)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-598)function str._getBoolean( boolean_str )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-599)local boolean_value[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-600)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-601)if type( boolean_str ) == 'string' then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-602)boolean_str = boolean_str:lower()[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-603)if boolean_str == 'false' or boolean_str == 'no' or boolean_str == '0'[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-604)or boolean_str == '' then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-605)boolean_value = false[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-606)else[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-607)boolean_value = true[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-608)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-609)elseif type( boolean_str ) == 'boolean' then[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-610)boolean_value = boolean_str[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-611)else[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-612)error( 'No boolean value found' )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-613)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-614)return boolean_value[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-615)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-616)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-617)--[[[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-618)Helper function that escapes all pattern characters so that they will be treated[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-619)as plain text.[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-620)]][](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-621)function str._escapePattern( pattern_str )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-622)return ( string.gsub( pattern_str, "[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%0" ) )[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-623)end[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-624)[](https://freemwiki.com/index.php?oldid=579572&title=Module%3AString#L-625)return str Retrieved from "[https://freemwiki.com/index.php?title=Module:String&oldid=579572](https://freemwiki.com/index.php?title=Module:String&oldid=579572)" * This page was last edited on 21 October 2025, at 06:18. * Content is available under [Creative Commons Attribution-ShareAlike 4.0](https://creativecommons.org/licenses/by-sa/4.0/) unless otherwise noted. FreemWiki is operated by **FINGERPOWER DIGITAL TECHNOLOGY LLC** (Colorado, Entity ID 20221978727). * [Privacy Policy](https://freemwiki.com/wiki/Privacy_Policy) * [About](https://freemwiki.com/wiki/About) * [Terms of Service](https://freemwiki.com/wiki/Terms_of_Service) * [Mobile view](https://freemwiki.com/index.php?title=Module:String&oldid=579572&mobileaction=toggle_view_mobile)