Question

I need to get rid of unwanted symbols, such as the multiple spaces, the leading and trailing whitespaces, as well as escape single and double quotes and other characters that may pose problems in my Neo4J Cypher query.

I currently use this (string.js Node module and jsesc Node module)

result = S(result).trim().collapseWhitespace().s;
result = jsesc(result, {   'quotes': 'double'  });

They work fine, however,

1) I want to find a better, easier way to do it (preferably without those libraries) ;

2) When I use other encodings (e.g. Russian), jsesc seems to translate it into some other encoding than UTF-8 that the other parts of my script don't understand.

So I wanted to ask you if you could recommend me a RegExp that would do the job above without me having to use those modules.

Thank you!

Was it helpful?

Solution

I have a series of regex replace calls that do what you seem to be looking for, or at least the issues you mentioned. I put together a test string with several items you mentioned.

var testString = ' I start  with \"unwanted items and" end with a space". Also I have Quotes ';
var cleanedString = testString.replace(/\s\s+/g, ' ').replace(/^\s|\s$/g, '').replace(/([^\\])(['"])/g, "$1\\$2");
console.log(cleanedString);

This will escape quotes (single or double) that have not yet been escaped, though you would have to worry about the case where the item is preceded by an escaped escape symbol. For example \\' would not be turned into \\\' as it should be. If you want to escape more characters you just need to add them to the final .replace regex. Let me know if there are specific examples you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top