Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Regex question

  1. #1
    New Coder
    Join Date
    Apr 2009
    Posts
    34
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Regex question

    The following code works as required -

    Code:
    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?

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts
    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

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

  • #3
    New Coder
    Join Date
    Apr 2009
    Posts
    34
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thanks Dean440, I now have
    Code:
    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.

  • #4
    New Coder
    Join Date
    Apr 2009
    Posts
    34
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Got it -

    Code:
    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;
    }

  • #5
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Code:
    return !/electronic|online|web/i.test( result.marc.f695.subfields['a'] );


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •