简体   繁体   中英

Regular expression for not accepting a series of Dots(.)

I am working with Javascript wherein I accept a Filename which is sent through an URL.

I have written a Regex to omit special characters.

    isFileNameValid: function (value)
    {                                 
        return new RegExp("([\\\:\*\?\"\<\>\|\/])").test(value);
    }

But I have failed to find a way to combine this with a regular expression which does not accept a string which has consecutive dots.

Any help is appreciable.

I am not quite sure what you want, because it seems like that is approving strings that contain one of those special characters (as opposed to stripping them out which you say you are doing).

But, if you want for this function to return false if your current regex is true and the string contains consecutive periods, you can use the following

[\\\\\\:\\*\\?\\"\\<\\>\\|\\/]|\\.{2,}

if you want the opposite logic, this should do it

[\\\\\\:\\*\\?\\"\\<\\>\\|\\/]|(?!.*(\\.{2,}))

I would highly recommend using a regex visualizer, like debuggex for this sort of thing - makes it much easier

In your case, I would just add another regex to the test function like

isFileNameValid: function (value)
{                                 
    return  /([:*?"<>|\/])/.test(value) && !/[.]{2}/.test(value);
}

As stated in the comment above - I don't think /([:*?"<>|\\/])/ is correct anyways. With that, you simply test if one of the characters : , * , ? , " , < , > , | , or / is in value and if so, it's a valid filename?!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM