JSFiddle - React, Tailwind, and code Playground
by Pankaj Kargirwar
JavaScript
function quoteRegex(str) {
let delimiter = '/';
//https://locutus.io/php/pcre/preg_quote/
return (str + '')
.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&')
}
function pregMatch(regex, sub, matches = []) {
//let r = /`${regex}`/;
//r = /select/
//let r = new RegExp(quoteRegex(regex));
let r = new RegExp(/`${regex}`/);
let m = sub.match(r);
console.log(m)
if (m == null) {
return 0
}
if (matches) {
m.forEach((e) => {
matches.push(e);
})
}
return 1
}
let query = "select * from table";
let matches = [];
if (pregMatch('^select', query, matches)) {
//if (pregMatch('^select', query, matches)) {
console.log("match")
} else {
console.log("not match")
}