RegExp and capital letters newbie question

Discussion in 'JavaScript' started by Mentalhead, Nov 22, 2010.

  1. #1
    Hey guys, I just started learning JavaScript and I was wondering can someone help me with this little thing.
    I have the following line of code that needs to check a string for capital letters:

    
    var b=1;
    var reci="BOG otac";
    var patt1=/[A-Z]/g;
    provera1 = patt1.test(reci.charAt(b-1));
    provera2 = patt1.test(reci.charAt(b));
    provera3 = patt1.test(reci.charAt(b+1));
    document.write(provera2);
    Code (markup):
    The problem is that variables provera1 and provera3 are giving me true value, but provera2 is always giving me false. I don't know what I'm doing wrong, and I don't know why provera2 is giving me false value, so can somebody explain it to me?
     
    Mentalhead, Nov 22, 2010 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    you are using the test reversed.

    
    var b=1;
    var reci="BOG otac";
    var patt1=/[A-Z]/g;
    provera1 = reci.charAt(b-1).test(patt1);
    provera2 = reci.charAt(b).test(patt1);
    provera3 = reci.charAt(b+1).test(patt1);
    
    Code (javascript):
     
    dimitar christoff, Nov 22, 2010 IP
  3. Mentalhead

    Mentalhead Active Member

    Messages:
    1,344
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    53
    #3
    I've tried your code, and it doesn't work. I don't know why.
     
    Mentalhead, Nov 22, 2010 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    dimitar christoff, Nov 22, 2010 IP