The value returned by a module function must always be a string, however some functions here return numbers (these are len, str_find, find and count). Could you…
| This module was considered for merging with Module:HTMLDecode on 2020 May 8. The result of the discussion was no consensus. |
| Text or other creative content from this version of Module:String was copied or moved into incubator:Module:Wp/nod/String with this edit. The former page's history now serves to provide attribution for that content in the latter page, and it must not be deleted as long as the latter page exists. |
This edit request has been answered. Set the |answered= parameter to no to reactivate your request. |
The value returned by a module function must always be a string, however some functions here return numbers (these are len, str_find, find and count). Could you please apply this diff? You can just copy and paste the code at this permanent link.
Although unnoticeable when used in normal wikitext, this can create problems when Module:String is invoked using other modules.
For instance, focusing on the len function, for each argument passed, a template named mytemplate containing the following code
{{#invoke:params|mapping_by_invoking|string|len|mapping_by_replacing|^.*$|%0 mod 3|1|for_each|[$#:$@]}}
should print [PARAMETER-NAME:LENGTH-OF-PARAMETER mod 3]
The code above invokes {{#invoke:string|len|...}} for each parameter passed. Then it attempts to replace the lengths saved with %0 mod 3, i.e. by adding mod 3 at the end of each parameter. And so, for instance, {{mytemplate|hello|world|foo|bar}} should print
However, since {{#invoke:string|len|...}} returns a number, any attempt to do string manipulation with the number returned will generate an error. --Grufo (talk) 05:17, 18 October 2024 (UTC)
The value returned by a module function must always be a stringis not true. mw:Extension:Scribunto/Lua reference manual#Returning text states
The module function should usually return a single string; whatever values are returned will be passed through tostring() and then concatenated with no separator.Further, when calling a module function from other Lua code even that doesn't apply; in that case it's like any other Lua function. I also note this change may well break other code that calls these functions (if it for some reason calls functions from this module instead of calling Scribunto's string manipulation functions directly) that expect a number from
len or the like. Anomie⚔ 11:12, 18 October 2024 (UTC)replace: empty strings are not recognizedHi. I noticed that the replace function is unable to recognize empty strings (see third example):
{{#invoke:string|replace|Foo|^.*$|Hello|1|false}}
{{#invoke:string|replace|Bar|^.*$|Hello|1|false}}
{{#invoke:string|replace||^.*$|Hello|1|false}}
--Grufo (talk) 10:47, 12 July 2025 (UTC)
find() but that code makes some sort of sense – find anything in an empty string should return 0. Makes me wonder if replace() was created after find() and used find() as an armature upon which to construct replace(). Seems to me that line 402 could be rewritten as: if '' == pattern then. But, are there any templates out there that rely on this anomaly?replace() was added on 24 February 2013, two days after function find() was added. The early return in if source_str == '' or pattern == '' [...] was added in between those edits: Special:Diff/540073010. —andrybak (talk) 14:10, 12 July 2025 (UTC)
{{#invoke:string|replace|2=^.*$|3=Hello|4=1|5=false}}), it is possible for there to be no parameter 1. I don't know what _getParameters would do with that but the code in str.replace should handle a situation where parameter 1 is nil. For convenience, the code treats nil and empty as the same and that might be part of the reasoning for returning an empty string. I agree that ^.*$ should match an empty string although, as mentioned above, it is possible that someone has taken advantage of this undocumented behavior. @WOSlinker: Any thoughts? Johnuniq (talk) 04:37, 13 July 2025 (UTC)
^.*$ so won't take long to check if the undocumented behaviour is used. -- WOSlinker (talk) 07:39, 13 July 2025 (UTC)
^X*$, X*, X? and of course an empty string will match another empty string, etc. There are certainly better ways to replace empty strings with nonempty ones but the logic is valid. The suggestion Trappist the monk made is not the right solution either because it ignores the replace text. Instead change the or to an and and change the return from source_str to replace. In fact, another optimization would be: inside if plain then add if pattern == source_str then return replace end. —Uzume (talk) 19:16, 16 July 2025 (UTC)
I am having trouble searching for vertical bar (|) i.e., U+007C; |. (My RW app: find |- in tables to identify the beginning of a table row.) Here are some tests trying to match c|d in a string containing abc|def that do not work, but I'm not sure how to specify the vertical bar either in the pattern. (Or, for that matter, in the string in the tests below; note that some of these use | or | although they render as vbar even embedded in nowikis, so what appear to be duplicate tests below are actually different):
{{#invoke:String|match|s=abc{{!}}def |pattern=c|d |plain=false |nomatch=0}} ⟶ 0{{#invoke:String|match|s=abc{{!}}def |pattern=c|d |plain=false |nomatch=0}} ⟶ 0{{#invoke:String|match|s=abc{{!}}def |pattern=c\|d |plain=false |nomatch=0}} ⟶ 0{{#invoke:String|match|s=abc|def |pattern=c{{!}}d |plain=false |nomatch=0}} ⟶ 0{{#invoke:String|match|s=abc|def |pattern=c\|d |plain=false |nomatch=0}} ⟶ 0{{#invoke:String|match|s=abc\|def |pattern=c\|d |plain=false |nomatch=0}} ⟶ c\{{#invoke:String|match|s=abc|def |pattern=c\|d |plain=false |nomatch=0}} ⟶ 0According to mw:LUAREF#Character class, | just represents itself, because it is not one of ^$()%.[]*+-?), but you can't place it into a pattern in {{#invoke:String|...}} because the invocation will treat it as a param separator. So, how do I search for it using string match, or in any of the other string functions that take patterns?
Using transcluded file /vbar test data, I was able to do it, thus:
{{#invoke:String|match|s={{/vbar test data}}|pattern=[a-z]{{!}}[a-z] |plain=false}} ⟶ c|d{{#invoke:String|match|s={{/vbar test data}}|pattern=[a-z]{{!}}[a-z] |plain=false |match=2}} ⟶ k|lBut I wasn't able to do it inline without transcluding a file. Is there a way? Mathglot (talk) 22:01, 6 October 2025 (UTC)
{{#invoke:string | match
| s = abc{{!}}def
| pattern = c{{!}}d
| plain = false
| nomatch = 0
}}
{{#invoke:string | match
| s = abc{{!}}def
| pattern = c{{!}}d
| plain = true
| nomatch = 0
}}
{{tjp2|plain|true}}). P.S. Template calls, parser functions and module invocations are expanded before parameters are passed; so if you write {{!}}, the match function will simply receive | and will have no idea you ever called a parser function. --Grufo (talk) 22:35, 6 October 2025 (UTC)
{{subst:#invoke:string | match
| s = abc{{!}}def
| pattern = c{{!}}d
| plain = true
| nomatch = 0
}}
match function will actually receive {{!}} instead of | (this is because substitutions are expanded before transclusions). And so, to pass | to the match function in substitutions you will have to write:{{subst:#invoke:string | match
| s = abc{{subst:!}}def
| pattern = c{{subst:!}}d
| plain = true
| nomatch = 0
}}
You are invited to join the discussion at Wikipedia:Village pump (technical) § Weird lua error from Module:String when substing {{undated}} on active disagreements in 3O. Snowmanonahoe (talk · contribs · typos) 05:23, 24 February 2026 (UTC)
E.g.:
^{{#invoke:String|replace|source=*foo}}
^
<nowiki /> at the start of the output, or using HTML entities like {{#invoke:String|replace|source=*foo}} instead. Anomie⚔ 13:26, 5 August 2026 (UTC)
* Here are the refs:
<!-- -->{{SAFESUBST:<noinclude />Replace|{{{refs}}}|
| }}}}
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.