1

I am trying to test a input string of format AWT=10:15;, based on this regex that I wrote: (([AWT]\w\S=)?(\d{0,9}:)?(\d{0,9});).

Problem 1: I am receiving this error: Uncaught TypeError: Cannot read property '0' of undefined when I try to match the string with the regex.

Problem 2: The string seems to come off as valid even after I enter this: AWT=10:15;12 which shouldn't be the case.

Here is my code:

var reg = new RegExp('(([AWT]\w\S=)?(\d{0,9}:)?(\d{0,9});)');
var x = $('td').find('.awt')[0].value; 

console.log(x);        // AWT=10:15;
console.log(String(x).match(reg)); // [";", ";", undefined, undefined, "", index: 9, input: "AWT=10:15;", groups: undefined]

       if(String(x).match(reg)){
            console.log("valid");
        }else{
            console.log("Invalid")
        }

I was wondering if anyone can help me figure out the right regex for the string.

PS: The string needs to be in that exact format: (AWT=[0,9]:[0,9];).

3 Answers 3

1

Your regex (([AWT]\w\S=)?(\d{0,9}:)?(\d{0,9});) means:

(                       # start group 1
    (                       # start group 2
        [AWT]                   # 1 character A or W or T
        \w                      # 1 word character <=> [A-Za-z0-9_]
        \S                      # 1 non space character
        =                       # equal sign
    )?                      # end group 2, optional
    (                       # start group 3
        \d{0,9}                 # from 0 upto 9 digits
        :                       # colon
    )?                      # end group 3, optional
    (                       # start group 4
        \d{0,9}                 # from 0 upto 9 digits
    )                       # end group 4
    ;                       # semicolon
)                       # end group 1

It matches AWT=10:15; as well as:

  • blahAWT=123456789:123456789;blah
  • Wx!=123;
  • 12345;

Not sure it's what you want!


According to your requierement “The string needs to be in that exact format: AWT=[0,9]:[0,9];”, this regex will do the job: ^AWT=\d\d:\d\d;$

Explanation:

^           # beginning of string
    AWT=        # literally AWT=
    \d\d        # 2 digits
    :           # a colon
    \d\d        # 2 digits
    ;           # a semicolon
$           # end of string

Use it this way:

var reg = new RegExp('^AWT=\\d\\d:\\d\\d;$');

or, better:

var reg = /^AWT=\d\d:\d\d;$/;
Sign up to request clarification or add additional context in comments.

Comments

0

@user120242 is totally right about the cause of your bug.

I just want to add another option instead of escaping the characters. If you use /someRegex/ this is not necessary.

Moreover, if you are always expecting AWT at the beginning, this ([AWT]\w\S=) is not optimal. It means: either A, W or T, then a word character, then a non space character and then =. If it always starts with AWT= use (AWT=).

var reg = /(([AWT]\w\S=)?(\d{0,9}:)?(\d{0,9});)/
var x = $('td').find('.awt')[0].value;

console.log(x); // AWT=10:15;
console.log(String(x).match(reg));

if (String(x).match(reg)) {
  console.log("valid");
} else {
  console.log("Invalid")
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td><input class="awt" value="AWT=10:15;"></td>
  </tr>
</table>

1 Comment

thank you for the answer! However, when I tried AWT=10:15;;; or AWT=10:15;73247 it was still giving me those as a valid string.. I would like the regex to be AWT=[0-9]:[0-9];. Anything in the middle besides an int value and anything after the last semi colon should be invalid.
0

You need to escape backslashes in the string, or \w becomes w
You need to use ^$ to match on beginning and end, so it only matches if the full string matches

Another option depending on what you are trying to do, is to use negative lookahead (?!.*)
or a capture group behind the last (;.*) where you would check matches for the final match group for content and reject based on that.

console.log('(([AWT]\w\S=)?(\d{0,9}:)?(\d{0,9});)'); // this is not what you want

function matcher(){
var reg = new RegExp('^(([AWT]\\w\\S=)?(\\d{1,9}:)?(\\d{1,9});)$');
var x = $('td').find('input')[0].value;

console.log(x); // AWT=10:15;
console.log(x.match(reg)); // [";", ";", undefined, undefined, "", index: 9, input: "AWT=10:15;", groups: undefined]

if (reg.test(x)) {
  console.log("valid");
} else {
  console.log("Invalid")
}
}

matcher();
$('td').find('input').on('input',matcher);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table><tr>
<td><input class="awt" value="AWT=10:15;"></td></tr></table>

2 Comments

This works great, however if I test this regex with AWT=; it still accepts it. That is supposed to be considered invalid.
Just change {0,9} to {1,9} so it requires at least one character

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.