@rbxts/jsnatives
search
Searches for a match between a pattern and a string.
Signature
function search(str: string, pattern: PatternAdmissible): number
Description
The search
function executes a search for a Lua pattern in a string and returns the index of the first match found. It uses Lua's pattern matching system rather than JavaScript's regular expressions.
Parameters
str
: The string to search within.pattern
: The pattern to search for. Can be a string, number, or boolean. If a non-string value is provided, it will be converted to a string.
Return value
- The zero-based index of the first match of the pattern.
- If no match is found, -1 is returned.
Examples
Basic usage
// Search for a simple substringconst index = String.search("Hello world", "world");print(index); // Outputs: 6
// Search for a characterconst letterIndex = String.search("Hello world", "e");print(letterIndex); // Outputs: 1
Using Lua patterns
// Search for any digitconst digitIndex = String.search("Hello 123 world", "%d");print(digitIndex); // Outputs: 6
// Search for any whitespaceconst spaceIndex = String.search("Hello world", "%s");print(spaceIndex); // Outputs: 5
// Search for word boundaryconst wordStart = String.search("Hello world", "%a+");print(wordStart); // Outputs: 0
Pattern matching examples
// Match a word starting with 'w'const wWordIndex = String.search("Hello world", "w%a+");print(wWordIndex); // Outputs: 6
// Match any vowelconst vowelIndex = String.search("Hello", "[aeiou]");print(vowelIndex); // Outputs: 1
// Match a pattern with character classesconst uppercase = String.search("Hello World", "%u%l+");print(uppercase); // Outputs: 0 (matches "Hello")
Not found cases
// Pattern not in the stringconst notFound = String.search("Hello world", "xyz");print(notFound); // Outputs: -1
// Pattern not matchingconst noMatch = String.search("12345", "%a");print(noMatch); // Outputs: -1 (no alphabetic characters)
Using non-string patterns
// Using a numberconst numberPattern = String.search("The answer is 42!", 42);print(numberPattern); // Outputs: 14
// Using a booleanconst boolPattern = String.search("Is it true or false?", true);print(boolPattern); // Outputs: 6
Common Lua pattern characters
// %d - digitsconst digits = String.search("abc123", "%d+");print(digits); // Outputs: 3
// %a - lettersconst letters = String.search("123abc", "%a+");print(letters); // Outputs: 3
// %w - alphanumericconst alphanumeric = String.search("!@#$abc123", "%w+");print(alphanumeric); // Outputs: 4
// %s - whitespaceconst whitespace = String.search("hello world", "%s");print(whitespace); // Outputs: 5
// . - any characterconst anyChar = String.search("hello", "h.l");print(anyChar); // Outputs: 0
Differences from JavaScript RegExp
// In JavaScript you might use:// "Hello world".search(/o/g) // returns 4
// In this API:const result = String.search("Hello world", "o");print(result); // Outputs: 4
// JavaScript regex flags aren't supported// For case-insensitive, use character classes insteadconst caseInsensitive = String.search("Hello", "[hH]");print(caseInsensitive); // Outputs: 0
Practical applications
// Validate if a string contains a numberfunction containsNumber(text: string): boolean { return String.search(text, "%d") !== -1;}
print(containsNumber("Hello123")); // Outputs: trueprint(containsNumber("Hello world")); // Outputs: false
// Check if a string starts with uppercasefunction startsWithUppercase(text: string): boolean { return String.search(text, "^%u") !== -1;}
print(startsWithUppercase("Hello")); // Outputs: trueprint(startsWithUppercase("hello")); // Outputs: false
// Find the position of the first wordfunction findFirstWord(text: string): number { return String.search(text, "%a+");}
print(findFirstWord(" Hello world")); // Outputs: 2print(findFirstWord("123Hello")); // Outputs: 3
Comparison with indexOf
const str = "Hello world";
// Using search for exact match (like indexOf)const searchResult = String.search(str, "world");print(searchResult); // Outputs: 6
// Using indexOf for comparisonconst indexOfResult = String.indexOf(str, "world");print(indexOfResult); // Outputs: 6
// The key difference: search can use Lua patternsconst patternResult = String.search(str, "w%a+"); // any word starting with 'w'print(patternResult); // Outputs: 6
// This would be more complex with indexOf