Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· OpenAIAi2 WildChat-4.8M (AI2)

js regex if condition tha data must be contains number:number AM OR PM - number:number…

  1. User

    js regex if condition tha data must be contains number:number AM OR PM - number:number AM OR PM , don't care by spaces

  2. Assistant

    You can use the following regular expression to check if the data contains the specified format:

    /^\s*(\d{1,2}:\d{2}\s*[AP]M)\s*-\s*(\d{1,2}:\d{2}\s*[AP]M)\s*$/
    

    This regex pattern looks for the following format:

    • First number:number followed by AM or PM
    • A hyphen with spaces on both sides
    • Second number:number followed by AM or PM

    You can use this regex pattern in JavaScript like this:

    const regex = /^\s*(\d{1,2}:\d{2}\s*[AP]M)\s*-\s*(\d{1,2}:\d{2}\s*[AP]M)\s*$/;
    
    const data = "12:30 PM - 3:45 AM";
    if (regex.test(data)) {
      console.log("Data is in the correct format");
    } else {
      console.log("Data is not in the correct format");
    }
    

    This code will check if the data variable contains the specified format and print a message based on the result.