views:

92

answers:

3

i have this code:

<script type="text/javascript">
var str="KD-R35H2UND";
var patt1=/[G|T|EE|EJU].*D/i;
document.write(str.match(patt1));
</script>

by using code at var patt1=... i can show like this:

if i type KD-R35ED => SHOW ED
KD-R35UND => UND
KD-R35JD => JD
KD-R35TJD => TJD
KD-R35EED=> EED

my problem is: if i type KD-R35GD it can show GD but if KD-R35D it can't show anything..how to make it works?

+2  A: 

b* means "match zero or more occurences of b".

. means "match any character except newline".

[EE|EJU] means "match an E, a |, a J or a U".

.* means "match any character except newline zero or more times".

D means "match a D".

So the regex is doing what you asked it to do.

From the examples you provided in your question, I'm guessing that the actual rules should be:

  1. String starts with KD-R35.
  2. Then any number of alphanumeric characters may follow, as long as
  3. there is an E, a J or a U among them, and
  4. the string ends in D.

These rules, as a regex, read:

^         # start of string
KD-R35    # literal text
(         # start of capturing group
  \w*     # any number of alphanumeric characters
  [EJU]   # at least one of E, J, or U
  \w*     # any number of alphanumeric characters
  D       # a D
)         # end of capturing group
$         # end of string

or, in JavaScript:

match = subject.match(/^KD-R35(\w*[EJU]\w*D)$/i);
if (match != null) {  // successful match
    mytext = match[1] // capturing group 1
} else {
    // Match attempt failed
}

I'm assuming that upper/lowercase don't matter.

EDIT: Erm, your new edit changes the rules. It seems that any string is allowed as long as it starts with KD-R35 and ends in D. In this case, the regex would simply be /^KD-R35(\w*D)$/i.

Tim Pietzcker
look at my edited question
klox
it seems like a same problem..always show KD-35H2UND,H2UND
klox
I don't understand. `match[0]` contains the entire string ("KD-R35H2UND"), `match[1]` contains the variable part ("H2UND"). It is not possible to match only `H2UND` if it's preceded by `KD-R35` because JavaScript doesn't support lookbehind assertions.
Tim Pietzcker
okey..thank's i agree with your answer..
klox
after all can work i've been found another problem..please check at my question..
klox
A: 

Is the first part of the string (KD-R35) same (or at least the format) for all instances of the string you wish to validate?

Can you write a list of string examples and the extractions you would like have after regex match.

For example:

  • KD-R35HASDF => HASDF
  • KD-R35H1234 => H1234
  • KD-R35HASDA => HASDA
  • KD-R35HG54G => HG54G
  • KD-R35HD23D => HD23D

If that is the case this regex might help:

.{2}\-.{3}(.*)

Meaning:

  1. any character 2 times
  2. minus sign
  3. any character 3 times
  4. CAPTURE - any character any number of times

HTML:

    <script type="text/javascript">
        var str1="KD-R35HASDF";
        var str2="KD-R35H1234";
        var str3="KD-R35HASDA";
        var str4="KD-R35HG54G";
        var str5="KD-R35HD23D";
        var patt1=/.{2}\-.{3}(.*)/;
        document.write(str1.match(patt1)[1] + "<br>");
        document.write(str2.match(patt1)[1] + "<br>");
        document.write(str3.match(patt1)[1] + "<br>");
        document.write(str4.match(patt1)[1] + "<br>");
        document.write(str5.match(patt1)[1] + "<br>");
    </script>

And note that the str5.match(patt1) returns an array, so use the second element like this:

str5.match(patt1)[1]

and you will get the result you are looking for

Miljenko Barbir
look at my edited question
klox
should be `.{2}-.{3}(.*)`, he wants everything after the `KD-R35` it seems.
beggs
no..it show KD-R35H2UND,H2UND...
klox
it returns an array, you need to use> str1.match(patt1)[1]
Miljenko Barbir
A: 
<script type="text/javascript">
var str="KD-R35H2UND";
var patt1=/.{2}-.{3}(.*)/;
document.write(str.match(patt1));
</script>

I change to this one but show KD-R35H2UND,H2UND

klox