import { fn } from "https://deno.land/x/ddc_vim@v4.0.2/deps.ts";
const { match } = fn;
When {expr} is a List
then this returns the index of the
first item where {pat} matches. Each item is used as a
String, Lists
and Dictionaries
are used as echoed.
Otherwise, {expr} is used as a String. The result is a Number, which gives the index (byte offset) in {expr} where {pat} matches.
A match at the first character or List
item returns zero.
If there is no match -1 is returned.
For getting submatches see matchlist()
.
Example:
:echo match("testing", "ing") " results in 4
:echo match([1, 'x'], '\a') " results in 1
See string-match
for how {pat} is used.
Vim doesn't have a strpbrk() function. But you can do:
:let sepidx = match(line, '[.,;: \t]')
Vim doesn't have a strcasestr() function. But you can add "\c" to the pattern to ignore case:
:let idx = match(haystack, '\cneedle')
If {start} is given, the search starts from byte index
{start} in a String or item {start} in a List
.
The result, however, is still the index counted from the
first character/item. Example:
:echo match("testing", "ing", 2)
result is again "4".
:echo match("testing", "ing", 4)
result is again "4".
:echo match("testing", "t", 2)
result is "3".
For a String, if {start} > 0 then it is like the string starts
{start} bytes later, thus "^" will match at {start}. Except
when {count} is given, then it's like matches before the
{start} byte are ignored (this is a bit complicated to keep it
backwards compatible).
For a String, if {start} < 0, it will be set to 0. For a list
the index is counted from the end.
If {start} is out of range ({start} > strlen({expr}) for a
String or {start} > len({expr}) for a List
) -1 is returned.
When {count} is given use the {count}'th match. When a match is found in a String the search for the next one starts one character further. Thus this example results in 1:
echo match("testing", "..", 0, 2)
In a List
the search continues in the next item.
Note that when {count} is added the way {start} works changes,
see above.
See pattern
for the patterns that are accepted.
The 'ignorecase' option is used to set the ignore-caseness of
the pattern. 'smartcase' is NOT used. The matching is always
done like 'magic' is set and 'cpoptions' is empty.
Note that a match at the start is preferred, thus when the
pattern is using "*" (any number of matches) it tends to find
zero matches at the start instead of a number of matches
further down in the text.
Can also be used as a method
:
GetText()->match('word')
GetList()->match('word')