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.
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]
);
Bookmarks