# Thread: Help on string pattern matching

1. ## Help on string pattern matching

I hope you guys can help me regarding string pattern matching in Javascript.

The first one checks if a particular string is in the following pattern:
"NNN-NN-NNNL" including the dashes (excluding the quotes); N here means any numeral from 0 to 9 while L means an UPPERCASE alphabet letter from A through Z except I and O.

The second one checks if a particular string is in the following pattern;
"YYYY-MMM-DD-N" format (including the dashes and excluding the quotes); YYYY means a valid 4-digit year, MMM means a valid short form for name of a month in capital letters, and DD is a valid day of the month and N still means any numeral from 0 to 9.

Since MMM means a valid short form for name of a month in capital letters, the following are the only allowed characters in MMM:
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
NOV
DEC

So what approach should I do to solve these problems?

2. This kind of string pattern matching can be done with Regular Expressions.

3. I think that I need to first to determine the length of the string (using .length). In the case of the first problem, the string length should equal to 11.
In the second problem, the string length should equal to 13.

If the string length matches, what should I do next? Can the power of regular expressions alone solve my first problem or do I have to 'split' each part of the text (using .substring()) into manageable parts?

4. You don't need to check the length of the String, use ^ and \$ to be sure the String matches your content.

And no, there is no need to either split the String or use substring when using a Regular Expression.

5. What you would need from the character table is the following:
• ^ to check that the String starts with what you defined
• \d to look for digital characters
• use the {n} to specify the exact amount of occurrences
• a character set [xyz] to define the allowed last character
• \$ to check that the String ends with what you defined

For your first example you would then define a Regular Expression looking like this:
Code:
`/^\d{3}-\d{2}-\d{3}-[A-HJ-NP-Z]\$/`
Now you can test the expression using the Regular Expression test method:
Code JavaScript:
```var re = /^\d{3}-\d{2}-\d{3}-[A-HJ-NP-Z]\$/;
var stringToEvaluate = "222-22-222-A";

6. Thanks a LOT, Pepejeria! I never thought that regular expression is that powerful!

So how about for my second problem, is it possible to determine if the MMM values in YYYY-MMM-DD-N is valid (such as JAN, FEB, MAR, etc.) via regular expression as well?

After determining via regular expression (if it is indeed possible), I think the string needs to split (via .substring()) in order to determine if the date is valid (via some date validation function). If the date is valid, then it passes all the tests. Is this the most appropriate approach to this?

7. Yes, you could check the MMM values using Regular Expressions:
Code:
`/JAN|FEB|MAR/`
etc... but you also want to check if the Date is valid. Meaning that it depends on the year and day.

What I would do would be to do two checks. The first one being a Regular Expression check to see if the correct characters are entered, then I would get the specific values using substring and create a Date object and see if it would work.

Though this seems like a very fiddly task when you could instead use dropdowns to let the user select a date.

8. Does the Date constructor in JavaScript automatically 'adjust' the date?

Take a look at the following code:
Code JavaScript:
```var myDate = new Date("FEB 29, 2007"); //Note that there's really no FEB 29, 2007 since 2007 is not a leap year

The output is:
Thu Mar 01 2007 00:00:00
What I'm asking is if there's a builtin date validator in JavaScript?

9. No, there is not, you will have to create a function that checks this.

How about the dropdown solution I mentioned above?

10. @Pepejeria: I know your drop down solution is more efficient but I like to increase my knowledge in regular expressions.

How about a pattern matching for a string with any combination of letters, numbers, and special characters? How should this pattern look like in regular expression?

In a pattern matching for a string with any combination of letters, numbers, this is what I did: (Please check if there's some error)
Code JavaScript:
`var alphaNumericPattern = /^[a-zA-Z0-9]+\$/;`

11. That is correct, it will accept a word character one or more times. There is a character class representing this: \w

The \w is equals to [a-zA-Z0-9_] (note that there is an underscore at the end).

12. @Pepejeria: How about a pattern matching for a string with any combination of letters, numbers, and special characters? How should this pattern look like in regular expression?

13. Code:
`/^\d{4}-[A-Z]{3}-\d{1,2}-\d\$/`
Without mentioning the dashes: The above matches 4 digits, 3 uppercase letters range A-Z, followed by 1 or 2 (meaning that 1 and 01 are ok) digits and ends with any number.

14. @Pepejeria: I mean is there a class for special characters in JavaScript regular expression?

I'm thinking of the following solution
Code JavaScript:
`var alphaNumericSpecialPattern = /^[a-zA-Z0-9`~!@#\$&#37;^&*()-=_+,./<>?;':"[]\{}|]+\$/;`
Did I do the pattern right?

15. Any character (except a line terminator) would be the "." character class (without the quotes). You need to escape the dot with a backslash if you want to really have a dot in there.

16. @Pepejeria: So my pattern now will be:
Code JavaScript:
`var alphaNumericSpecialPattern = /^[.]+\$/;`

Basically, I want to add the following special characters to my character set:
`~!@#\$&#37;^&*()-=_+,./<>?;':"[]\{}|

will the above JavaScript code yield the result that I want?

17. You can omit the brackets, they are only used to group more than one character.

Your expression means: it can start with any character at least 1 time. Any character means all the ones you specified plus &#229;&#246;&#228; etc.

#### Posting Permissions

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