Sunday, May 15, 2011

Javascript Regular expression example for lifein

var cname ="Visit hellow big Ben Test";
var search_txt = "Big ben RamRaj";
var searchOpt = "Big ben RamRaj";
searchOpt = searchOpt.replace(/ /gi, "|");


cname = cname.toLowerCase();
search_txt = search_txt.toLowerCase();
searchOpt = searchOpt.toLowerCase();

console.log(searchOpt);

var rg = new RegExp(search_txt,'i');
var rgOpt = new RegExp(searchOpt,'i');
if (cname.search(rg)>=0) {
console.log('exists option 1');
} else if (cname.search(rgOpt)>=0) {
console.log('exists option 2');
} else {
console.log('not exists '+search_txt);
}

1 comment: