admin管理员组

文章数量:1289703

I have to validate the local folder path which is in the following format : ..\sentinel\log .

I have old regular expression ( /[\w]:\.*/)) for C:\sentinel\log and that was working. I need to make accept this path.

I have the following expresion from regexplibrary

var pathRE = new RegExp("/^((../|[a-zA-Z0-9_/-\])*.[a-zA-Z0-9])"); Error : SyntaxError: unterminated parenthetical

When i executing is throw this error

I am attaching the code that i have tried

function checkFolderpath(path) {
    try {
        //var pathRE = new RegExp(/[\w]:\\.*/);
        var pathRE = new RegExp("/^((\.\./|[a-zA-Z0-9_/\-\\])*\.[a-zA-Z0-9])");
        if (pathRE.test(path)) {
            $("#spanloggererror").html("");
            return true;
        }
        else {
            $("#spanloggererror").html(resx_Invalid_Loggerpath);
            valtemp = 1;
        }
        return false;
    }
    catch (err) {
        alert(err.Message);
    }

Please suggest me how to fix the issue.

Edit :

value of path : ..\Sentinel\log

I have to validate the local folder path which is in the following format : ..\sentinel\log .

I have old regular expression ( /[\w]:\.*/)) for C:\sentinel\log and that was working. I need to make accept this path.

I have the following expresion from regexplibrary

var pathRE = new RegExp("/^((../|[a-zA-Z0-9_/-\])*.[a-zA-Z0-9])"); Error : SyntaxError: unterminated parenthetical

When i executing is throw this error

I am attaching the code that i have tried

function checkFolderpath(path) {
    try {
        //var pathRE = new RegExp(/[\w]:\\.*/);
        var pathRE = new RegExp("/^((\.\./|[a-zA-Z0-9_/\-\\])*\.[a-zA-Z0-9])");
        if (pathRE.test(path)) {
            $("#spanloggererror").html("");
            return true;
        }
        else {
            $("#spanloggererror").html(resx_Invalid_Loggerpath);
            valtemp = 1;
        }
        return false;
    }
    catch (err) {
        alert(err.Message);
    }

Please suggest me how to fix the issue.

Edit :

value of path : ..\Sentinel\log

Share Improve this question asked Sep 26, 2013 at 14:01 Thangamani PalanisamyThangamani Palanisamy 5,3004 gold badges35 silver badges39 bronze badges 2
  • what is the value of path – Arun P Johny Commented Sep 26, 2013 at 14:02
  • The / is an issue at the start – epascarello Commented Sep 26, 2013 at 14:03
Add a ment  | 

2 Answers 2

Reset to default 6

Your regular expression should be constructed like this:

var pathRE = /^((..\/|[a-zA-Z0-9_/-\\])*.[a-zA-Z0-9])/;

The only time you really need to use the RegExp constructor is when you're building up a regular expression from separate pieces, dynamically. You have to be careful with quoting forward-slash characters in the expression (/) when you use native regular expression syntax. You don't have to quote them inside [ ] groups, but you do need to double your backslashes.

That regular expression won't match ..\what\ever because it only looks for forward slash at the start. It also won't match a terminal file name longer than two characters. I think a better one would be:

var pathRE = /^\.\.(?:\\[A-Za-z0-9_-]+)+/;

with appropriate changes for the file name characters you expect.

Escape the slash:

/^((\.\./|[a-zA-Z0-9_\/\-\\])*\.[a-zA-Z0-9])/
//          here   __^   and    add slash __^

本文标签: javascriptSyntaxError unterminated parenthetical for validating the local file pathStack Overflow