Regex in javascript
I needed to create a toggle image in a menu. I decided to check the image src to see what image it was on. Regex in javascript to the rescue. Here is what I used to parse only the image name.
var re = /([\w\/\.\\:]*\/)([\w\.]*)/;
var imgSrc = $('toc').src;
var myArray = re.exec(imgSrc);
if (myArray[2] == "toc_open.png") {
$('toc').src = "images/toc.png";
}
else {
$('toc').src = "images/toc_open.png";
}
For those familiar with prototype or jquery, I am using shorthand notation to grab the image with id of 'toc'. Then if the image is toc_open.png, I change it to toc.png, if not, I change it to toc_open.png. After running re.exec on the string, the actual file name was contained in the myArray['2'] due to the capturing parenthesis.
