简体   繁体   English

正则表达式字符串不以特殊字符开头或结尾

[英]Regex String Doesn't Start or End With Special Character

SO buddies, greetings.伙计们,你们好。 I have some password requirements I need to implement and one of the requirements is the string cannot start or end with a special character .我有一些需要实施的密码要求,其中一项要求是字符串不能以特殊字符开头或结尾 I did spend some time Googling around but my RegEx kung-fu is kimosabe level.我确实花了一些时间谷歌搜索,但我的 RegEx 功夫是 kimosabe 级别。

Just in case you're interested in some code, here's the JavaScript :以防万一您对某些代码感兴趣,这里是JavaScript
Note: Yes, passwords are also validated on the server as well:) The following snippet runs the RegEx tests and simply checks or x's the row item associated with the password rule.注意:是的,也在服务器上验证密码:) 以下代码片段运行 RegEx 测试并简单地检查或 x 与密码规则关联的行项目。

var validate = function(password){
        valid = true;

        var validation = [
            RegExp(/[a-z]/).test(password), RegExp(/[A-Z]/).test(password), RegExp(/\d/).test(password), 
            RegExp(/[-!#$%^&*()_+|~=`{}\[\]:";'<>?,./]/).test(password), !RegExp(/\s/).test(password), !RegExp("12345678").test(password), 
            !RegExp($('#txtUsername').val()).test(password), !RegExp("cisco").test(password), 
            !RegExp(/([a-z]|[0-9])\1\1\1/).test(password), (password.length > 7)
        ]

        $.each(validation, function(i){
            if(this == true)
                $('.form table tr').eq(i+1).attr('class', 'check');
            else{
                $('.form table tr').eq(i+1).attr('class', '');
                valid = false
            }
        });

        return(valid);

    }

EDIT: The regular expression you want is:-编辑:你想要的正则表达式是:-

/^[a-zA-Z0-9](.*[a-zA-Z0-9])?$/

Additional information附加信息

In regular expressions ^ means 'beginning of string' and $ means 'end of string', so for example:-在正则表达式中, ^表示“字符串的开头”, $表示“字符串的结尾”,例如:-

/^something$/

Matches火柴

'something'

But not但不是

'This is a string containing something and some other stuff'

You can negate characters using [^-char to negate-] , so您可以使用[^-char to negate-]否定字符,所以

/^[^#&].*/

Matches any string that doesn't begin with a # or a &匹配任何不以 # 或 & 开头的字符串

/^[a-zA-Z0-9](.*[a-zA-Z0-9])?$/

This expression checks the following validations:此表达式检查以下验证:

  • No blank spaces at start and end开头和结尾没有空格
  • No special characters at the end and beginning结尾和开头没有特殊字符
  • special characters allowed in between中间允许的特殊字符

This regex should be what you want:这个正则表达式应该是你想要的:

/^[0-9a-z].*[0-9a-z]$/

暂无
暂无

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

相关问题 验证不以特殊字符开头或结尾的文本框 - validate a textbox that Doesn't Start or End With Special Character 正则表达式删除所有以特殊字符开头的字符串 - Regex remove all string start with special character 什么是正则表达式 (Regex) 检查以 $ 开头且不以特殊字符结尾的字符串 - What would be Regular Expressions (Regex) to check string for start with $ and does not end with special character 使用正则表达式匹配字符串的相同开始和结束字符 - Match the same start and end character of a string with Regex 正则表达式删除字符串中字符的开始到结尾 - Regex expression to remove start to end of character in string 正则表达式-我希望我的字符串以2个特殊字符结尾 - Regex - I want my string to end with 2 special character 正则表达式以相同的字符串开头和结尾,而不仅仅是相同的字符 - Regex start and end with same string, not just same character REGEX用于查找不以空格开头或以空格结尾的单词 - REGEX for finding the word that doesn't start with space or end with space 正则表达式分隔字符串的开头和结尾 - Regex delimit the start of a string and the end 正则表达式不允许在字符串开始或结束处使用“.”、“_”、“-”,并且不应有连续的“.” 其余的所有特殊字符都不应被允许 - Regex to not allow '.', '_', '-' at String Start or End and should not have consecutive '.' and the rest all special characters should not be allowed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM