diff --git a/index.js b/index.js index c22cc6d..a613dc3 100644 --- a/index.js +++ b/index.js @@ -23,21 +23,20 @@ exports.parse = function (s, env) { ; } else if (/^"/.test(s)) { - return interpolate(s.replace(/^"|"$/g, '')) + return interpolate(s.replace(/^"|"$/g, '')); } else return interpolate(s); }) ; - function getVar (_, x) { - return String(env[x]) - } - function interpolate (s) { return s .replace(/\\([ "'\\$`(){}!#&*|])/g, '$1') - .replace(/\$(\w+)/g, getVar) - .replace(/\${(\w+)}/g, getVar) + .replace(/(^|[^\\])\$(\w+)/g, getVar) + .replace(/(^|[^\\])\${(\w+)}/g, getVar) ; } + function getVar (_, pre, key) { + return pre + String(env[key]); + } }; diff --git a/test/env.js b/test/env.js index 20ff7da..b9f9001 100644 --- a/test/env.js +++ b/test/env.js @@ -4,7 +4,7 @@ var parse = require('../').parse; test('expand environment variables', function (t) { t.same(parse('a $XYZ c', { XYZ: 'b' }), [ 'a', 'b', 'c' ]); t.same(parse('a${XYZ}c', { XYZ: 'b' }), [ 'abc' ]); - t.same(parse('a${XYZ}c $XYZ', { XYZ: 'b' }), [ 'abcb b' ]); + t.same(parse('a${XYZ}c $XYZ', { XYZ: 'b' }), [ 'abc', 'b' ]); t.same(parse('"-$X-$Y-"', { X: 'a', Y: 'b' }), [ '-a-b-' ]); t.same(parse("'-$X-$Y-'", { X: 'a', Y: 'b' }), [ '-$X-$Y-' ]); t.same(parse('qrs"$zzz"wxy', { zzz: 'tuv' }), [ 'qrstuvwxy' ]);