1

I want to validate following text using regular expressions

integer(1..any)/'fs' or 'sf'/ + or - /integer(1..any)/(h) or (m) or (d)

samples :

1) 8fs+60h
2) 10sf-30m
3) 2fs+3h
3) 15sf-20m

i tried with this

function checkRegx(str,id){
    var arr = strSplit(str);
    var regx_FS =/\wFS\w|\d{0,9}\d[hmd]/gi;

    for (var i in arr){
            var str_ = arr[i];
            console.log(str_);
            var is_ok = str_.match(regx_FS);
            var err_pos = str_.search(regx_FS);                
            if(is_ok){
              console.log(' ID from ok ' + id);
              $('#'+id).text('Format Error');
              break;
            }else{
              console.log(' ID from fail ' + id);
              $('#'+id).text('');
            } 
     }  
 }            

but it is not working

please can any one help me to make this correct

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2768851
  • 93
  • 1
  • 1
  • 5
  • 2
    It is difficult to understand what you want the regular expression to match. Perhaps a plain word explanation along with some examples of matching input might help. – Tibos Sep 11 '13 at 13:30
  • I always use this site for matching up regex for a start http://txt2re.com/index-php.php3 – Zesar Sep 11 '13 at 13:36

3 Answers3

1

I don't see how the expression you tried relates anyhow to the description you gave us. What you want is

/\d+(fs|sf)[+-]\d+[hmd]/

Since you seem to know a bit about regular expressions I won't give a step-by-step explanation :-)

If you need exclude zero from the "integer" matches, use [1-9]\d* instead. Not sure whether by "(1..any)" you meant the number of digits or the number itself.

Looking on the code, you

function checkRegx(str, id) {
    var arr = strSplit(str);
    var regx_FS = /^\d+(fs|sf)[+-]\d+[hmd]$/i;

    for (var i=0; i<arr.length; i++) {
        var str = arr[i];
        console.log(str);
        if (regx_FS.test(str) {
            console.log(' ID from ok ' + id);
            $('#'+id).text('Format Error');
            break;
        } else {
            console.log(' ID from fail ' + id);
            $('#'+id).text('');
        } 
    }  
}

Btw, it would be better to separate the validation (regex, array split, iteration) from the output (id, jQuery, logs) into two functions.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

This should do it:

/^[1-9]\d*(?:fs|sf)[-+][1-9]\d*[hmd]$/i

You were close, but you seem to be missing some basic regex comprehension.

First of all, the ^ and $ just make sure you're matching the entire string. Otherwise any junk before or after will count as valid.

The formation [1-9]\d* allows for any integer from 1 upwards (and any number of digits long).

(?:fs|sf) is an alternation (the ?: is to make the group non-capturing) to allow for both options.

[-+] and [hmd] are character classes allowing to match any one of the characters in there.

That final i allows the letters to be lowercase or uppercase.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Try something like this:

/^\d+(?:fs|sf)[-+]\d+[hmd]$/i
Scalpweb
  • 1,971
  • 1
  • 12
  • 14
  • 1
    Please notice the difference between character classes and (capturing) groups! Btw, your "integer" does not match the `60` from the example – Bergi Sep 11 '13 at 13:36
  • looks quite similar to Bergi's answer now – John Dvorak Sep 11 '13 at 13:39
  • True, haven't seen it sorry. Should I delete my post ? – Scalpweb Sep 11 '13 at 13:39
  • 1
    @Scalpweb There's not necessarily a reason to delete your answer if you independently came to the same answer as another answer. There are some potentially meaningful differences between the answers anyway. – ajp15243 Sep 11 '13 at 13:54