Skip to content

Instantly share code, notes, and snippets.

@chjj
Created November 2, 2011 00:52
Show Gist options
  • Save chjj/1332513 to your computer and use it in GitHub Desktop.
Save chjj/1332513 to your computer and use it in GitHub Desktop.
node functions for .bashrc
# $ echo "console.log('hello world')" | runjs
runjs() {
cat | xargs -0 node -e
}
# usage:
# $ echo "hello world" | jsrx "/h\w+/g" "hi"
jsrx() {
local str=$(cat | sed "s/\\\/\\\\\\\/g" \
| sed "s/'/\\\'/g" | sed ':a;N;$!ba;s/\r\n\|\n/\\n/g')
local pat="$1"
local sub="$2"
echo "process.stdout.write('${str}'.replace(${pat}, '${sub}') + '\n')" | runjs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment