最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - regex for the first word is day of the week - Stack Overflow

programmeradmin1浏览0评论

Can anyone suggest a regex that will test if the first word of a string is a day of the week.

e.g. "Tuesday, July 16"

So far I have this:

/^((Monday)|(Tuesday)|(Wednesday)|(Thursday)|(Friday)|(Saturday)|(Sunday))/.test("Tuesday, July 16")

Which does work, is there a way I can further use the regex to split the expression out into day and month?

Can anyone suggest a regex that will test if the first word of a string is a day of the week.

e.g. "Tuesday, July 16"

So far I have this:

/^((Monday)|(Tuesday)|(Wednesday)|(Thursday)|(Friday)|(Saturday)|(Sunday))/.test("Tuesday, July 16")

Which does work, is there a way I can further use the regex to split the expression out into day and month?

Share Improve this question asked Jul 9, 2013 at 17:04 dagda1dagda1 29k67 gold badges255 silver badges477 bronze badges 1
  • 2 Do you also care whether the date is a valid date or not? – Jerry Commented Jul 9, 2013 at 17:31
Add a ment  | 

2 Answers 2

Reset to default 4

Try this:

 /^(?i)\W*(?:Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day\b[\s\S]*$/.test("Tuesday, July 16")

Personally, I'd want to break this out into multiple pieces . . . one to check the formatting and one to check the validity of the data in the value.

First, check for the formatting, using a basic regex:

var input = "Tuesday, July 16";
var pattern = new RegExp("^([a-z]+), ([a-z]+) (\\d{2})$", "i");
var isFormattedCorrectly = pattern.test(input);

Then, if it is formatted correctly, I'd store off the valid day and month values in two arrays and check to see if the pieces of the date matched one of those values:

var validDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var validMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var isValidDay = false;
var isValidMonth = false;

if (isFormattedCorrectly ) {
    var dateParts = input.match(pattern);
    isValidDay = (validDays.indexOf(dateParts[1]) > 0) ? true : false;
    isValidMonth = (validMonths.indexOf(dateParts[2] > 0) ? true : false;
}

Note: If you are using JQuery, you can also us JQuery.inArray() instead of .indexOf().

While it may not be "short and sweet" I have a few of reasons for suggesting this approach:

  1. As I mentioned before, it allows you to separate the formatting and data checks from each other, which is generally considered a good thing. :)
  2. It supports more detailed validation messaging (e.g., "The date must be formatted as . . .", "You must enter a valid month", etc.), as opposed to a single, generic "This is not a valid date" message.
  3. Hopefully, it allows for easier upkeep of the "valid values" data, since it is simply an array, rather than a pattern built into the regex . . . you can update values at will (e.g., if you want to use 3-character month and day values instead of the full words).
  4. It allows for easy language customization (e.g., if you need to support Spanish for some reason, you could switch the data in your arrays be the Spanish Days and Months, and the check would still work).
发布评论

评论列表(0)

  1. 暂无评论