Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:07:42+00:00 2026-06-15T07:07:42+00:00

I have a string var str = 2 Days, 2 Hours 10 Minutes; if

  • 0

I have a string

var str = "2 Days, 2 Hours 10 Minutes";

if I :

str.split(/Days/);

I get:

["2 ", ", 2 Hours 10 Minutes"]

So I think I can use string.split to split out my string and get the “days”, “hours” and “minutes” with this method.

However, what do I do when I sometimes have “s” or no “s”, for example:

var str = "1 Day, 2 Hours 10 Minutes";

Here my string is “1 Day” not “2 Days” so I don’t have the “s” on the “day” value. Is there a way to split a string on

Day(s)
Hour(s)
Minute(s)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:07:44+00:00Added an answer on June 15, 2026 at 7:07 am

    You’re using a Regular Expression to do the split, so simply place a question mark after the ‘s’ to make it optional:

    str.split(/Days?/);
    

    You probably also want to make it case-insensitive by adding the i flag to the Regex:

    str.split(/days?/i);
    

    You could also do the whole thing with a single line using the match string method (instead of split) like this:

    ("2 Days, 3 hours, 5 minutes").match(/(\d+)\s*days?\,?\s*(\d+)\s*hours?\,?\s*(\d+)\s*minutes?/i)
    

    That will return an array like this:

    ["2 Days, 3 hours, 5 minutes", "2", "3", "5"]
    

    So the items in positions 1, 2 and 3 are the days, hours and minutes respectively.

    To break that Regex down into english:

    • (captured group, i.e. array item 1) 1 or more digits
    • zero or more whitespace characters
    • day
    • optional s
    • optional comma
    • zero or more whitespace characters
      … and repeat for hours and minutes.

    Check the docs for regex matching on strings here, and great docs on how to use regular expressions here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
I have string like this var str = #it itA itB _ #et etA
I have a string like this: var str = I'm a very^ we!rd* Str!ng.;
I will have a string like below var str = -#A This text belongs
I have a string. It's just like; var str = This is an example
I have a string like this as below var str = '101,1245###company1-|102,1221###company2-|102,1651###company3-|-'; Can we
I have a string variable var str = 'this is eeeeeeee'; . I want
ok, I guess the code says it all. //I have this string. var str
HI I have a URL var str:String = conn=rtmp://server.com/service/&fileId=myfile.flv or var str:String = fileId=myfile.flv&conn=rtmp://server.com/service/
I have the following situation: function Mach3Code(Str: String): String; var StrOut: String; begin StrOut

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.