Ported BBCode from PHP to JavaScript