Match String w/ Uppercase or Lowercase Characters?

I’ve found myself in a particular problem where I need to see if there are any matching characters in a string regardless of if they’re uppercase or lowercase.

I could write something to do that manually, but I’m wondering if Roblox already has a way to do this with string patterns or something.


local text = 'Hello World!'
local textToMatch = 'world'

Which outputs nil obviously. I should clarify that I need to remember that the W in World is capitalized. Using string.lower is not ideal as I want to use string.gsub on the matching text to highlight the results using RichText.

Why don’t you do

local Text = "Hello World!"

--Check for matches, get start and end of matching text
local Start, End = string.find(string.lower(Text), "world")

if Start then
    --Get non-string.lower'd text from original string
    local Matching = string.sub(Text, Start, End)
    print(Matching) --Prints "World" and not "world"
1 Like

That’s what I wanted to avoid as I would need to make this recursive until no more matches are found. While I can do this, I’m just wondering if there’s an easier/better method using something like string patterns.

I don’t think I understand what you want

helloworld richtext

local Text = "Dummy: Hello beatiful world!"

local CheckFor = {

local Label = script.Parent

for _, Word in pairs(CheckFor) do
	local Start, End = string.find(string.lower(Text), Word)

	if Start then
		local Matching = string.sub(Text, Start, End)
		local NewText = string.sub(Text, 1, Start - 1) .. '<font color="rgb(255,165,0)">' .. Matching .. '</font>' .. string.sub(Text, End + 1, #Text)
		Text = NewText
		Label.Text = NewText
1 Like

Not sure why the first solution wouldn’t work for you. What are you trying to do exactly? Roblox doesn’t add more functionality to the string library.

That is what I want, but I was hoping there was a way to just use string.gsub with some string pattern that would let it include matching text regardless of capitalization. But if not, I guess I’ll have to do that.

Also I meant that there may be more than one matching word. As in, there could be multiple ‘worlds’ in the text with varying capitalization. So I’d have to recursively check until there is none left. I was hoping to not have to do this, as stated above, but I guess I don’t really have a choice.

My knowledge on string patterns is still a little shoddy but from what I do know you’re either looking to use a plain text match or string patterns. Neither of them are really interchangeable so you need to work that into your own code accordingly.

I personally think that you should be using lowercase on the string so you can find both ends of the match and then apply it back on the real string. Alternatively since you’re using gsub you can supply it a function to see if the match meets your word and apply your adjustments as needed:

local text = 'Hello World!'
local textToMatch = 'world'

-- Globally substitute matches of "%a+" (any upper or lower character)
-- by the given function (matches: "Hello", "World")
string.gsub(text, "%a+", function(match)
    -- If the match in lowercase (Hello, World) is equal to the textToMatch in
    -- lowercase (world), then proceed with substitution
	if string.lower(match) == string.lower(textToMatch) then
		print(match) --> World

Just as a silly point out, this wouldn’t work for your case because it doesn’t arbitrarily match – it only applies a match to the first letter but not to the rest, so if this can’t match as “[A-Za-z]orld” it would fail. Maybe it’ll spark an idea in your head but best not to think too much of it. The first thing I mentioned, giving a function to gsub, should work for your case.

print(string.match(text,"[%a]orld")) --> World