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 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

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2538
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 1, 20222022-06-01T05:16:38+00:00 2022-06-01T05:16:38+00:00

javascript – Check whether a given substring is present in a string, if so get the value inside a parenthesis next to it

  • 0

[ad_1]

  • There are two operations, one is which regex to use, second is how to get the + 12-17 when we have validate the string.

  • I suggest that the string like “Date (xx-xx), Date (xx-xx), ..”;

The regex I use to test if there is wednesday:

^(.*Wednesday)(.*\((([0-9])([0-9])(-)([0-9])([0-9]))\))
  • I will not explain the regex carefully, it;s upto you

The complete source code and description

    let string = "jhdishd, Wednesday (12-17), thgudhfuids(), jhfsd, dfjshfjkd, fsdujhf";
    let regex = /^(.*Wednesday)(.*\((([0-9])([0-9])(-)([0-9])([0-9]))\))/;
    let result = "";

    //Test the regex
    if (regex.test(string) == true)
    {
        //Split all " ," in the string
        //The array will contain string like : arr[0] Date (xx-xx), ..
        let arrayOfElement = string.split(', ');

        for (let i = 0; i < arrayOfElement.length; ++i)
        {
            //Test if the date have wednesday
            if (regex.test(arrayOfElement[i]) == true)
            { 
               //Split the ( in Date (xx,xx))
               arrayOfElement2 = arrayOfElement[i].split("(");

               //The result of arrayOfElement2[1] is xx-xx)
               //Just get the xx-xx and ingore )
               let resultString = arrayOfElement2[1].substring(0, 5);

               result = resultString;
           }
       }
   }
   console.log(result);

Input

jhdishd, Wednesday (12-17), thgudhfuids(), jhfsd, dfjshfjkd, fsdujhf

Output

12-17
  • Update : for monday, tuesday, .., just change the regex, which is wednesday to monday, tuesday, ..

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 9 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 11 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 10 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

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.