Reputation: 2058
I have a SQL string:
var sql= "AND DT_FIM IS NULL AND ( CD_JOB, DT_INI_JOB ) IN (SELECT x.CD_JOB, x.DT_INI FROM PRS_JOBS_MAQUINA x WHERE x.CD_JOB = ':CD_JOB' AND TO_CHAR(x.DT_INI, 'YYYY-MM-DD') = ':DT_INI_JOB' AND x.DT_FIM IS NULL)
and i need to extract the binded values(:CD_JOB , :DT_INI_JOB), the problem is that with
var bindFields=sql.substring(sql.lastIndexOf("':") + 1 , sql.lastIndexOf("'"));
it returns only the first match and i need both. Is it possible with Javascript? Lodash to the rescue if anybody find it useful.
https://jsfiddle.net/oq0dmyjo/1/ . Apreciate your help . Thanks
Upvotes: 1
Views: 90
Reputation: 627087
You can use a very simple regex with a capturing group:
/':([^']+)/g
Explanation:
':
- a literal sequence ':
([^']+)
- Group 1 capturing 1 or more symbols other than '
g
- a global modifier matching all instances.Here are some resources to learn:
JS code:
var re = /':([^']+)/g;
var str = "AND DT_FIM IS NULL AND ( CD_JOB, DT_INI_JOB ) IN (SELECT x.CD_JOB, x.DT_INI FROM PRS_JOBS_MAQUINA x WHERE x.CD_JOB = ':CD_JOB' AND TO_CHAR(x.DT_INI, 'YYYY-MM-DD') = ':DT_INI_JOB' AND x.DT_FIM IS NULL)";
var res = [];
while ((m = re.exec(str)) !== null) {
res.push(m[1]);
}
document.body.innerHTML = "<pre>" + JSON.stringify(res, 0, 4) + "</pre>";
Upvotes: 2
Reputation: 33496
Since it looks like you're binding the fields, you probably want to use replace
with a replacement function. Use the regex from @WiktorStribiżew, /':([^']+)'/g
, but then use it like this:
var sql = "AND DT_FIM IS NULL AND ( CD_JOB, DT_INI_JOB ) IN (SELECT x.CD_JOB, x.DT_INI FROM PRS_JOBS_MAQUINA x WHERE x.CD_JOB = ':CD_JOB' AND TO_CHAR(x.DT_INI, 'YYYY-MM-DD') = ':DT_INI_JOB' AND x.DT_FIM IS NULL)";
var fields = {
CD_JOB: 1,
DT_INI_JOB: 2
};
sql = sql.replace(/':([^']+)'/g, function($0, $1) {
return fields[$1];
});
console.log(sql);
Upvotes: 0