sql-match
Match a string using an SQL pattern.
This library is basically a spec-compliant implementation of a LIKE
between two strings:
SELECT 'string' LIKE '%ing'; --> true
Supported features:
%
wildcard sequence_
wildcard\
escape
Unsupported features:
- Custom escape character (
ESCAPE
) - Ignored trailing spaces (MySQL's
=
) - Disallowed trailing escape character (PostgreSQL)
- Collated international characters (
COLLATE
with=
) [charlist]
patterns (Access and SQL Server)?
and#
wildcards (Access)
Installation
Node.js >= 8
is required. To install, type this at the command line:
npm install sql-match
Usage
isSQLMatch(pattern, testString)
const isSQLMatch = ; ; //-> true ; //-> true; //-> true; //-> true ; //-> true; //-> true; //-> true
Optionally, you can create a reusable/cacheable regular expression to improve performance:
const sqlToRegex = ; const pattern = ; 'string''stringing';//-> true
Gotchas
Because JavaScript strings are interpreted, you may want to use String.raw
to avoid some annoyances that reduce consistency with SQL.
Non-wildcard escape sequences are possible:
; //-> true; //-> true// or; //-> true; //-> true
SELECT 't' LIKE '\t'; --> true SELECT 'u0020' LIKE '\u0020'; --> true
Matching a literal wildcard will require you to escape the escape character:
; //-> true// or; //-> true
SELECT '%trin_' LIKE '\%trin\_'; --> true
Matching a literal backslash will require you to escape the escaped escape character:
; //-> true// or; //-> true
SELECT '\string' LIKE '\\string'; --> true