JavaScript splits & matches with regular expressions (regex)

I had been developing some client-side validation code in jQuery/JavaScript and using Firefox (and the excellent Firebug) to test and debug it. I was then asked to ensure that it worked in IE6 & IE7 and that's when the problems started.

Apart from the usual "which file does that line number equate to, and why does it not tie up?" issues I found that IE doesn't like taking a regular expression as it's parameter to the JavaScript split function. Firefox will happily accept this and works fine but IE doesn't. After some searching it appears that Firefox might be the odd one out and that it's non-standard to pass in a regex.

So what do you do if you want to split up a string based on a regular expression or rather a rule that can't be simply expressed in the way that the split function wants it? Wouldn't it be nice if you could ask if a string matches a regex but then use certain matched bits of the string in your next few lines of code?

Well you can, simply use the match method, surrounding the bits of the regex that you want to use later in parenthesis '(' and ')' and then you can use the global JavaScript variable RegEx to pull them out.

So if 1234-ABC is your text, and you want the numbers as one part and the characters as another then you would use this regular expression to match on: ^([0-9]*)-([A-Z]*)$. You can then get hold of the matched numbers bit with RegEx.$1 and the letters bit with RegEx.$2.

var productCode = "1234-ABC";
productCode.match(/^([0-9]*)-([A-Z]*)$/);
var numbers = RegEx.$1;
var letters = RegEx.$2;
Technorati Tags: , ,

How to disable the auto-completion 'bell' in Cygwin (using RXVT)

If you have ever hit TAB a few times in bash (via RXVT) you will probably be greeted with the loudest 'bell' your PC can muster. After a while this gets pretty annoying so here's how to disable it if you are using RXVT inside Cygwin (this might work for other Cygwin terminals, I've just not checked)

Navigate to your home directory (normally just by typing cd and either edit or create a file called .inputrc

Add the following lines to the .inputrc file:

# Disable the annoying bell
set bell-style none
Save the file, close the terminal and reopen - you should now be bell-less!

Technorati Tags: , ,

How to get Scala working with the RXVT terminal on Cygwin

Out of the box Scala support Cygwin, but this is only with the Windows command prompt-based bash terminal. If you have opted for the more UNIX-like terminal of RXVT then you will find that although the interactive Scala interpreter runs, you can't get it to do anything!

This has been raised as a bug (Ticket #2097) against the Scala project and graehl even posted a patch to changed the generation of the scala runtime scripts.  As my Scala install was based on the downloaded Windows binaries (scala-2.7.6.final.zip) I couldn't directly use this patch, but I could examine it to see what graehl's fix was.

It appears that the key bit was to add the following Java command line option to the java statement that starts the scala interactive interpreter:

-Djline.terminal=jline.UnixTerminal
So the last line of my bin/scala file is:
exec "${JAVACMD:=java}" $JAVA_OPTS -cp "$TOOL_CLASSPATH" -Dscala.home="$SCALA_HOME" -Denv.classpath="$CLASSPATH" -Denv.emacs="$EMACS" -Djline.terminal=jline.UnixTerminal scala.tools.nsc.MainGenericRunner  "$@"
This appears to work, my interactive environment is now interactive!

Technorati Tags: , , ,

Tommy Emmanuel being VERY creative with an acoustic guitar and some clever delay

I don't often post about guitar stuff as I really want to keep this blog focused on the tech side of my life but I couldn't help but pass this YouTube link on:

Delay (& Creative Uses for It)

I started to watch it and was soon completely captivated by it, hope you like it too!

Technorati Tags: , ,