I receive a file path string via JSON.parse
, but I need to escape the spaces in the string with backslashes.
How should I do this idiomatically in Node.js?
For example:
var input = JSON.parse('{ "path": "/foo/bar bam/baz.html" }');
input.path.replace(/(\s)/g, '\\$1');
You just need: string. replace(/ \//g,"\n"); so that you're matching a space followed by an (escaped) forward slash.
var input = JSON.parse('{ "path": "/foo/bar bam/baz.html" }');
console.log(input.path.replace(/(\s+)/g, '\\$1'));
Update your .replace
regular expression as follows:
var input = JSON.parse('{ "path": "/foo/bar bam/baz.html" }');
console.log(input.path.replace(/(\s+)/g, '\\$1'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With