0
var existing = "";   
if(disk.isLinux){   
    var valinvalid = "/usr" ;            
    var valinput = /^\/[a-zA-Z]{2,}/ ;  
    if(!valinput.match(valinvalid)){      
        return "^/" + existing + "[^/][a-zA-Z]{2,}[^/]$";
    } 
}

Here im trying to do the following in the first if condition ie. if(disk.isLinux):
1. there should be minimum 3 characters
2. the first character should be /
3. the entire input shouldnt match "/usr". But it can be /us or /usra

4
  • Could you postr some examples of what should match and what shouldn't? Commented Feb 28, 2012 at 14:47
  • Other than the default linux directories like /usr, /root etc... anything else with minimum 3 characters and starting with / is a valid input Commented Feb 28, 2012 at 14:50
  • Try running it on regular-expressions.info/javascriptexample.html. It worked for me on Firefox and Chrome Commented Feb 28, 2012 at 14:57
  • mgibsonbr: thanks for the reforemat :) Commented Feb 28, 2012 at 14:57

3 Answers 3

1

If you are just trying to test if it matches, us test on regexp:

/^\/[\w]{2,}/.test("/usr/");     //true

Is this what you are trying to do?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you guys for your support return "^/" + existing + "(?!usr$)[a-zA-Z]{0,}$";- This worked.
0

A couple of things:

1) vars should never ever ever be inside if statements

2) String.prototype.match exists RegExp.prototype.match does not

But more importantly, you dont need regEx at all

if (
 input.length < 3 ||
 input.charAt(0) !== '/' ||
 input === '/user'
) {
 throw new Error("I'm not happy with the input");
}

Comments

0

try changing your code to use:

var valinput = new RegExp("/^\/[a-zA-Z]{2,}/") ;  
 if(!valinput.test(valinvalid)){

Comments

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.