JavaScript regex for the first word is day of the week regex first word before space,regex first word in string,

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?

Answer:1

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).
Answer:2

I'm using the iterate mediator for saving files. For this I need a counter for the iterations. I tried to create an property outside of the iteration and use the script mediator to count the ...

I'm using the iterate mediator for saving files. For this I need a counter for the iterations. I tried to create an property outside of the iteration and use the script mediator to count the ...

  1. wso2 esb iterate counter

Im working with jquery mobile in my project and what i trying to do instead to use the swipe effect, use two button to change to the next and to the previous data-role=page. im trying with this ...

Im working with jquery mobile in my project and what i trying to do instead to use the swipe effect, use two button to change to the next and to the previous data-role=page. im trying with this ...

  1. jquery mobile change page
  2. jquery mobile change page programmatically
  3. jquery mobile change page on button click
  4. jquery mobile change button text
  5. jquery mobile change background color
  6. jquery mobile change button color
  7. jquery mobile change theme
  8. jquery mobile change icon color
  9. jquery mobile change
  10. jquery mobile change theme dynamically
  11. jquery mobile change page with parameter
  12. jquery mobile change header color
  13. jquery mobile change selected option
  14. jquery mobile change select value
  15. jquery mobile change event
  16. jquery mobile change hash
  17. jquery mobile change css
  18. jquery mobile change data theme dynamically
  19. jquery mobile change page event
  20. jquery mobile change font size

Following is my JSON file: <?php $driver_booking = q("select * from vehicle_driver_booking where vehicle_id = ".$_REQUEST['ch']); $array = array(); ...

Following is my JSON file: <?php $driver_booking = q("select * from vehicle_driver_booking where vehicle_id = ".$_REQUEST['ch']); $array = array(); ...

I have a store which I want to hardcode some initial values into. Then when it loads from the server I want it to APPEND the results to the initial values, rather than replacing them. Is this possible?...

I have a store which I want to hardcode some initial values into. Then when it loads from the server I want it to APPEND the results to the initial values, rather than replacing them. Is this possible?...