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 832013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:17:43+00:00 2026-05-15T04:17:43+00:00

how to modify this code if i want search JD from var str=KD-S35JWD ..i

  • 0

how to modify this code if i want search JD from var str="KD-S35JWD"..i try this but doesn’t work:

<script type="text/javascript">

var str = "KD-R435jwd";
var hasUD;
var hasJD;
var hasED;
var hasEED;
var patt1 = str.match(/U/gi);
var patt2 = str.match(/J/gi);
var patt3 = str.match(/E/gi);
var patt4 = str.match(/EE/gi);
var patt5 = str.match(/D/gi);

if (patt1 && patt5) {
    hasUD = 'UD';
    document.write(hasUD);
} else if (patt2 && patt5 {
    hasJD = 'JD';
    document.write(hasJD);
} else if (patt3 && patt5) {
    hasED = 'ED';
    document.write(hasED);
} else { 
       hasEED = 'EED';
        document.write(hasEED);

</script>
  • 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-05-15T04:17:44+00:00Added an answer on May 15, 2026 at 4:17 am

    Your posted code is correct, besides one points: In JavaScript, elseif should be else if.

    If it possible that a string has both combination of characters, you may want to remove the else and have two ifs:

    if (patt1 && patt3) {
        hasUD = 'UD';
        alert(hasUD);
    } 
    if (patt2 && patt3) {
        hasJD = 'JD';
        alert(hasJD);
    }
    

    Another option is to use a more specific pattern. Currently you check for the characters anywhere in the string. For example, the D in KD- is enough to satisfy your condition. Depending on your specification:

    1. D is always after U: if(str.match(/U\w*D$/i))
    2. D is always last: if(str.match(/U\w*D$/i))
    3. D and U are on the same block (any order): if(str.match(/U\w*D|D\w*U/i))

    Another option is to use a single match for all cases:

    var matches = str.match(/([JU])\w*(D)/i);
    if(matches){
        var firstLetter = matches[1];
        var secondLetter = matches[2];
        alert(firstLetter + secondLetter);
    }
    else {
        alert(":(");
    }
    

    One last note: you don’t need the /g flag in this case. The global flag is used to find all occurrences of the patters, but here you’re only checking for one.

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

Sidebar

Related Questions

I'm trying to modify the code from this script . Basically I'm trying to
I am looking to modify this php code to do a recursive search for
I want to modify this method (implement my own version of it) but I
I have this big code where I want 3 things in my search: 1-
I have this code about binary search tree , I want Calculated efficancy for
Is there an easy way to modify this code so that the target URL
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
I found this PHP code in an app I have to modify... $links =
I keep getting this error while trying to modify some tables. Here's my code:
Looking at this jQuery example, how can I modify the code so that it

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.