How to parse search query with quotes within code

Viewed 97

I have a search query which is working in the database client but when using it in nodejs having quotes parsing error.

Here is the original query

   match (n)-[l:DATA_FLOWS]->(m) where l.nme =~ '(?i).*new.*' return n, l, m

I'm trying use it in coding.

session.run('match (n)-[l:DATA_FLOWS]->(m) where l.nme =~ ''(?i).*'{feedParam}'.*' return n, l, m', {feedParam:search.value}).then(function (result) {

How to complete the quotes to make it work?

2 Answers

The whole regex has to be passed as parameter :

session.run('match (n)-[l:DATA_FLOWS]->(m) where l.nme =~ ''(?i).*'{feedParam}'.*' return n, l, m', {feedParam:search.value})
.then(...)

Should be changed to :

var regexStr = '(?i).*' + search.value + '.*'
session.run('MATCH (n)-[l:DATA_FLOWS]->(m) WHERE l.nme =~ {feedParam} RETURN n, l, m', {feedParam: regexStr})
.then(...)

** update with more than one parameter ** :

var regexStr = '(?i).*' + search.value + '.*'
var source = 'BoxA'
var destination = 'Box J'
var query = `MATCH (n)-[l:DATA_FLOWS]->(m) WHERE l.nme =~ {feedParam}
AND n.nme = $source AND m.nme = $destination 
RETURN n, l, m`
session.run('', {feedParam: regexStr, source: source, destination: destination})
    .then(...)

You can concatenate strings with + and you can use ' or " for strings.

So replace ' with '+"'"+'

In your case, you aren't using " so you can just put the string in those

session.run("match (n)-[l:DATA_FLOWS]->(m) where l.nme =~ '(?i).*new.*' return n, l, m", {feedParam:search.value})

Related