Skip to content

A regex pattern to check whether a string contains unescaped single quotes.

Notifications You must be signed in to change notification settings

matwerber1/regex-to-detect-unescaped-single-quotes-for-sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

Purpose

Provide a regex pattern that will match an entire string if, anywhere within the string, it contains an unescaped single quote. An "escaped single quote" is defined as two consecutive quotes.

I use this regex to help detect malformed inputs, aka input sanitization, for SQL queries.

Put another way, this will match an entire string if, anywhere within the string, there is an odd number (1, 3, 5, etc) of adjacent single quotes.

Regex

This was provided by a colleague and regex wizard, Nicolas Moutschen. Big thanks to Nicolas, my regex skills are not as savvy :)

^.*(?<!')'('')*(?!').*$

Examples

You can test this out on https://regex101.com/

Matches

This should match the entire string from any of these examples:

This is a ' test
This ' is another ' test
This is a ''' test that has one escaped quote and one adjacent unescaped quote
This matches if, anywhere within the string, there is ' an odd number of ''''' quotes

AppSync Resolver Example

#set($myInputIsValid = !($util.matches("^.*(?<!')'('')*(?!').*$",$ctx.args.myInput)))

#if (!$myInputIsValid)
    $util.error("Input contains unescaped quote.")
#end

About

A regex pattern to check whether a string contains unescaped single quotes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published