...

View Full Version : Regex question



blinks
06-10-2010, 04:38 AM
The following code works as required -



if (result.marc.f695.subfields['a'] != "Electronic journals"
&& result.marc.f695.subfields['a'] != "electronic journals") {
return;
}



However, this isn't the most efficient way to code it, especially as I want to add further possibilities, most beginning with "[Ee]lectronic. I know I need to use regexes, and think the code I need to use is something like /[Ee]lectronic/, but I use JavaScript so rarely that I'm not sure how to build this into the above expression. Can anybody help?

Dean440
06-10-2010, 04:55 AM
test() is a method of RegExp objects that compares your regular expression pattern with a string and returns true or false. Your pattern is correct, but you can make it easier with the "i" flag (for case-insensitive) so that any permutation of "Electronic" works like ElEcTrOnIc


var pattern = /electronic/i;
pattern.test("my string"); //returns false

blinks
06-10-2010, 05:45 AM
Thanks Dean440, I now have


var pattern1 = /electronic/i;
var pattern2 = /online/i;
var pattern3 = /web/i;
if (pattern1.test(result.marc.f695.subfields['a'])
&& pattern2.test(result.marc.f695.subfields['a'])
&& pattern3.test(result.marc.f695.subfields['a'])) {
return;
}

However, still not working quite right as content that doesn't include electronic/online/web is still getting through.

blinks
06-11-2010, 12:05 AM
Got it -



var pattern1 = /electronic/i;
var pattern2 = /online/i;
var pattern3 = /web/i;
var patterntomatch = result.marc.f695.subfields['a'];
if (!pattern1.test(patterntomatch)
&& !pattern2.test(patterntomatch)
&& !pattern3.test(patterntomatch)) {
return;
}

randomuser773
06-11-2010, 03:47 AM
return !/electronic|online|web/i.test( result.marc.f695.subfields['a'] );



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum