RegExp

From PlainOldWebserver

Jump to: navigation, search

RegExp()

RegExp() is loved by Perl programmers, but ignored by Java programmers. Available options are 'g', 'i' and 'm'. Using the RegExp object instead of match() alone allows greater flexibility.


 var data = "If=2\nAnd=5\nBut=6";
 var find = "and";
 var re = new RegExp("("+find+")=(\\d+)","i");
 var m = data.match(re);
 if(m) {
   document.writeln("Match is "+m[1]);
 }

Result:

 Match is And

Never user RegExp() when split will do. When speed is an issue, split turns out to be much faster.

Personal tools
Navigation