How to put quotation marks inside of a string like " "text" " to check

obraz
obraz

its correct

in that case try using a regex pattern like this

local pattern = '[\'"].*(Game).*[\'"]'
print(string.match('Game.ReplicatedStorage', pattern))  -- no match
print(string.match('"Game is cool"', pattern))  -- match

@TheBaconHero_101 @T34P07D3V My function had a small typo, try this:

local function isTextInQuote(text)
	return string.match(text, '^".*"$') ~= nil
end
1 Like

obraz

obraz

now its correct

As an improvement, if anyone would prefer spaces to be ignored, this can be done:

local function isTextInQuote(text)
	return string.match(text, '^%s*".*"%s*$') ~= nil
end

print(isTextInQuote("Hello"))
print(isTextInQuote("  Hello  "))
print(isTextInQuote('"Hello"'))
print(isTextInQuote('  "Hello" ')) --this also works with this code

And here are a few details on how this works for people new to string.match:

  • ^ and $ make it so we check the entire string instead of any portion of it
  • %s* means that we’re OK with having any number of whitespace, including 0
  • " means that we have to match one "
  • .* means that we’re OK with having any number of characters, including 0

Another improvement:

local function isTextInQuote(text)
	return string.match(text, '^%s*["\'][^"\']*["\']%s*$') ~= nil
end

This one supports single quotes and disallows using another quote (unfortunately it disallows both types of quotes).