package
Match a string using an SQL pattern.
npm install sql-matchGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
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'; --> trueSupported features:
% wildcard sequence_ wildcard\ escapeUnsupported features:
ESCAPE)=)COLLATE with =)[charlist] patterns (Access and SQL Server)? and # wildcards (Access)Node.js >= 8 is required. To install, type this at the command line:
npm install sql-matchisSQLMatch(pattern, testString)
const {isSQLMatch} = require('sql-match');
isSQLMatch('string', 'string'); //-> true
isSQLMatch('%ing', 'string'); //-> true
isSQLMatch('s%ng', 'string'); //-> true
isSQLMatch('str%', 'string'); //-> true
isSQLMatch('_tring', 'string'); //-> true
isSQLMatch('st__ng', 'string'); //-> true
isSQLMatch('strin_', 'string'); //-> trueOptionally, you can create a reusable/cacheable regular expression to improve performance:
const {sqlToRegex} = require('sql-match');
const pattern = sqlToRegex('%ing');
['string','stringing'].every(testString => pattern.test(testString));
//-> trueBecause 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:
isSQLMatch('\t', ' '); //-> true
isSQLMatch('\u0020', ' '); //-> true
// or
isSQLMatch(String.raw`\t`, 't'); //-> true
isSQLMatch(String.raw`\u0020`, 'u0020'); //-> trueSELECT 't' LIKE '\t'; --> true
SELECT 'u0020' LIKE '\u0020'; --> trueMatching a literal wildcard will require you to escape the escape character:
isSQLMatch('\\%trin\\_', '%trin_'); //-> true
// or
isSQLMatch(String.raw`\%trin\_`, '%trin_'); //-> trueSELECT '%trin_' LIKE '\%trin\_'; --> trueMatching a literal backslash will require you to escape the escaped escape character:
isSQLMatch('\\\\string', '\\string'); //-> true
// or
isSQLMatch(String.raw`\\string`, String.raw`\string`); //-> trueSELECT '\string' LIKE '\\string'; --> trueEstimated unpacked size across 8 packages.
155.3 kB
Includes dependencies.