Captain's Universe Home
Captain's Universe Home
Cosmic Ray Muon DetectorTeleGarden Pages
Time on MarsBryophyllum Plants
Jupiter Radio AstronomyAncient Pages
Salzburg Tourist GuideEarth Magnetometer
  H O M E     AJAX & MORE     LINUX & MORE     RTAI     XENOMAI     ADEOS IPIPE      
    JAVA & BROWSERS     *NIX     ELECTRONICS     REVIEWS     ARTEMIA     FAIRY SHRIMP      


Javascript RegEx Regular Expression

These are a few handy javascript regular expressions functions, also for AJAX applications - RegEx'es in perl or PHP are very common, but when it comes to javascript it is slightly different and the resources on the net are quite scarce.

Matching a string (PHP: preg_match ; Perl s///):
mystring = "captain";
if (mystring.match(/cap.*?/)) {
	alert("match");
}


Spliting a string with regular expressions (Perl/PHP: split):
mystring = "captain|was|here";
myarray = mystring.split(/\|/);
for (var i = 0; i < myarray.length; i++) {
	alert(myarray[i]);
}

Replacing a string (PHP: preg_replace):
mystring = "captain was here";
myregexp = /was here/;
newstring = mystring.replace(myregexp, "is the best");
alert(newstring);


Using the RegExp object for matching and detecting the matches themselfs:
var regex = new RegExp("cap(.*?) (.*?) here");
var match = regex.exec("captain was here");
if (match == null) {
	alert("No match");
} else {
	var string = "matched at position " + match.index + ":\n";
	string = string + "string matched: " + match[0] + "\n";
	if (match.length > 0) {
		for (var i = 1; i < match.length; i++) {
			string = string + match[i] + "\n";
		}
	}
	alert(string);
}



Last-Modified: Sat, 04 Feb 2006 16:03:08 GMT

Google
 
Web www.captain.at
go to top
© 1996-2010 . All rights reserved.
No reproduction, distribution, publishing or transmission of the copyrighted materials at this site is permitted. Policy
go to top