Skip to content

Instantly share code, notes, and snippets.

/pattern/flags
new Regexp(pattern[, flags])

flags: g global, i ignore case, m multiline

regex.test(string) or string.search(regex) - returns true or false

regex.exec(string) - null or an array. 0 index is matched string, 1+ is captures. Maintains state for continuation searches /g flag is set.

@dy-dx
dy-dx / pre-commit
Last active January 21, 2016 23:31
Pre-commit hook that prevents debugging code and merge artifacts from being committed.
#!/bin/bash
# Pre-commit hook that prevents debugging code and merge artifacts from being committed.
FILES_PATTERN='\.(rb|erb|haml|js|coffee)(\..+)?$'
FORBIDDEN=( "binding\.pry" "save_and_open_page" "debugger" "it\.only" "describe\.only" ">>>>>>" "<<<<<<" "======" )
# the exit code from `grep -E $FILES_PATTERN` gets swallowed unless the pipefail option is set
set -o pipefail
// this would actually be in a beforeEach or something
// just haven't done that yet
var angular = require('angular');
var app = angular.module('app', [])
.directive('someDirective', require('./myDirective'));
module.exports = app;
@piscisaureus
piscisaureus / pr.md
Created August 13, 2012 16:12
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

@samyakbhuta
samyakbhuta / my_server.js
Created August 12, 2011 06:28
Catching an uncaughtException in express.js based server
var express = require('express');
process.on('uncaughtException', function(err) {
console.log( " UNCAUGHT EXCEPTION " );
console.log( "[Inside 'uncaughtException' event] " + err.stack || err.message );
});
anHTTPServer = express.createServer(function (req,res){
if (Math.random() > 0.9) throw new Error('fail!');
res.send("Hello World : Express!!");
});
@adammiller
adammiller / douglasPeucker.js
Created February 14, 2011 16:54
Javascript implementation of the Douglas Peucker path simplification algorithm
var simplifyPath = function( points, tolerance ) {
// helper classes
var Vector = function( x, y ) {
this.x = x;
this.y = y;
};
var Line = function( p1, p2 ) {
this.p1 = p1;