简体   繁体   English

不接受一系列Dots(。)的正则表达式

[英]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. 我正在使用Javascript,其中我接受通过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 但是,如果要让此函数在当前正则表达式为true并且字符串包含连续句点的情况下返回false,则可以使用以下命令

[\\\\\\:\\*\\?\\"\\<\\>\\|\\/]|\\.{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 我强烈建议您使用regex可视化工具(例如debuggex)来进行此类操作-使其更容易

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?! ,或/value ,如果是,则它是有效的文件名?!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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