www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Match exact number of uppercase letters

  1. #1
    Join Date
    Oct 2007
    Posts
    371

    resolved [RESOLVED] Match exact number of uppercase letters

    I want to match 5 uppercase letters followed by a dash, followed by 5 more uppercase letters. I tested this:
    Code:
    <script type="text/javascript">
    var str="^This is a test. PIYRW-NKJDQ"; 
    var patt1=/[A-Z]{5}-[A-Z]{5}/g;
    document.write(str.match(patt1));
    </script>
    and if I change the last part of str to "XXPIYRWV-XNKJDQ" it matches
    "IYRWV-XNKJD" when I want it to return null. What am I doing wrong?

  2. #2
    Join Date
    Nov 2002
    Location
    London UK
    Posts
    154
    Code:
    var str="^This is a test. PIYRW-NKJDQ"; 
    var patt1=/[A-Z]{5}-[A-Z]{5}$/g;
    document.write(str.match(patt1));
    Note the bold $ symbol, this indicates that the match much occur at the end of the string. If you try it now it will work fine.

  3. #3
    Join Date
    Dec 2010
    Posts
    207
    Presumably you want the match not to be preceded by an uppercase letter.

    Perhaps:
    Code:
    /(^|[^A-Z])[A-Z]{5}-[A-Z]{5}/g

  4. #4
    Join Date
    Oct 2007
    Posts
    371
    Quote Originally Posted by MikeOS View Post
    Note the bold $ symbol, this indicates that the match much occur at the end of the string. If you try it now it will work fine.
    Thanks for the reply.
    Take a look here:
    Code:
    <script type="text/javascript">
    var str="This is a test. PIYRW-NKJDQ but it doesn't do what I want"; 
    var patt1=/[A-Z]{5}-[A-Z]{5}$/g;
    document.write(str.match(patt1));
    
    var str1="This is a test. PIYRWXX-NKJDQYY but it doesn't do what I want"; 
    var patt1=/[A-Z]{5}-[A-Z]{5}$/g;
    document.write(str.match(patt1));
    I'd like the first one to return PIYRW-NKJDQ, but the second one to return null.

    maybe it would help if I explained what I want to do.
    I am trying to recognize an item number in a search string. If someoone searches for "HAPPY-HOLIDAYS", it would match "HAPPY-HOLID" incorrectly) If they type "ABCDE-FDSAA yellow" I'd like the item number to still be recognized.

  5. #5
    Join Date
    Aug 2009
    Posts
    593
    Try this expression:

    Code:
    /(^| )([A-Z]{5}-[A-Z]{5})( |$)/
    That will check that the words you are looking for are either at the beginning of the sentence, at the end, or standing alone with at least 1 space on either side.
    I did these examples to make sure it worked:

    Code:
    var str = "This is a test. PIYRW-NKJDQ but it doesn't do what I want";
    document.write(
    	($match = str.match(/(^| )([A-Z]{5}-[A-Z]{5})( |$)/)) === null ? 'NO MATCHES FOUND' : $match[2]
    );
    
    document.write('<br />');
    
    var str = "This is a test. PIYRWXX-NKJDQYY but it doesn't do what I want";
    document.write(
    	($match = str.match(/(^| )([A-Z]{5}-[A-Z]{5})( |$)/)) === null ? 'NO MATCHES FOUND' : $match[2]
    );

  6. #6
    Join Date
    Oct 2007
    Posts
    371
    Thanks:

    Let's see if I read this right
    Code:
    /(^| )([A-Z]{5}-[A-Z]{5})( |$)/
    first match is begin or space, second match is capital letters {exactly 5}, hyphen, capital letters {exactly 5}, third match is space or end.

    Then we use the second match.

    Is that right?
    I might understand this stuff after all!

    For my own reference, the next time I look up this thread:
    http://www.w3schools.com/jsref/jsref_obj_regexp.asp
    Last edited by TecBrat; 01-13-2011 at 10:24 AM. Reason: add link

  7. #7
    Join Date
    Oct 2007
    Posts
    371
    I am far from a wiz, but thanks to the couple of answers you've given me, I am well on my way to writing my own expressions.

    Thanks again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles