Regular expression search not working

Discussion in 'Javascript' started by donpro, Aug 12, 2011.

  1. donpro

    donpro Guest


    I have a TEXTAREA where I wish to ensure that it contains at least on
    4 digit number. The following code snippet always returns -1 even when
    the 4 digit number is present and I don;t know why. Note that I first
    ensure that the vale is not empty but I am not showing that code in
    the snippet below.

    var hsCodeElement = document.getElementById('TaField));
    var hsCodePattern = '/^\d{4}$/';

    if ( == -1) {
    alert( + ": HS Codes must
    contain at least entered one 4 digit numeric value");
    setTimeout(function() {hsCodeElement.focus();}, 10);
    donpro, Aug 12, 2011
    1. Advertisements

  2. Drop the single quotes, a regular expression literal in Javascript s
    delimited by slashes i.e.
    var hsCodePattern = /^\d{4}$/;
    I am not sure however you want the achors ^ and $ as you say "contains
    at least on[sic] 4 digit number" while the expression you have written
    basically says "is exactly a 4 digit sequence" and contains nothing
    before or after that.

    if (hsCodePattern.test(hsCodeElement.value)) {

    seems easier.
    Martin Honnen, Aug 12, 2011
    1. Advertisements

  3. If you mean "at least one sequence of 4 digits"

    var hsCodePattern = /\d{4}/;

    If you mean "at least one sequence of exactly 4 digits"

    var hsCodePattern = /(^|\s)\d{4}(\s|$)/;

    a) Don't quote the regex, it's a regex, not a string containing a regex.
    b) If you include start and end anchors, your pattern is treated more
    like "consists of exactly 4 digits" rather than "contains a 4 digit

    This html will allow you to evaluate the contents of a text area against
    4 different "find 4 digit" regexes.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    <meta http-equiv="Content-Type"
    content="text/html; charset=utf-8">
    <meta name="MSSmartTagsPreventParsing" content="TRUE">
    <title>Test RegExes</title>
    <script type="text/javascript">
    var regExes = new Array (/^\d{4}$/, /(^|\s)\d{4}(\s|$)/,
    /\s\d{4}\s/, /\d{4}/);
    function dgebi(id)
    return document.getElementById(id);
    function testRegExes()
    var ta = dgebi('theTextArea');
    var re, alert;
    for (re = 0; re < regExes.length; re++)
    alert = dgebi("alert_" + re);
    if (ta.value.match(regExes[re]))
    { = "#00ff00";
    { = "#ff0000";
    <style type="text/css">
    p {margin: 20px auto 0 auto; text-align: center;}
    table {margin: 20px auto 0 auto; border-collapse: collapse;}
    th, td {text-align: center;border: thin solid black;}
    <body onload="testRegExes()">
    <textarea cols="60" rows="6" id="theTextArea"></textarea><br>
    <input type="button" value="Evaluate" onclick="testRegExes()">
    <table width="80%">
    <td width="25%" id="alert_0">/^\d{4}$/</td>
    <td width="25%" id="alert_1">/(^|\s)\d{4}(\s|$)/</td>
    <td width="25%" id="alert_2">/\s\d{4}\s/</td>
    <td width="25%" id="alert_3">/\d{4}/</td>
    <table width="30%">
    <th width="100%">Color Key</th>
    <td style="background-color: #00ff00">RegEx Matched</td>
    <td style="background-color: #ff0000">RegEx Not Matched</td>


    Denis McMahon
    Denis McMahon, Aug 12, 2011
  4. That's really "at least one sequence of exactly 4 digits surrounded by
    whitespace or start or end of string"; for example, "a1234" does not
    match, and neither does "1234-5" or "1234.".

    If you want "at least one sequence of 4 digits that is not part of a
    longer digit sequence", then the pattern /(^|\D)\d{4}($|\D)/ would do.
    Jukka K. Korpela, Aug 12, 2011
  5. Or /\b\d{4}\b/

    Lasse Reichstein Nielsen, Aug 12, 2011
  6. No, that would be different; "a1234" would not match.
    Jukka K. Korpela, Aug 12, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.