简体   繁体   English

正则表达式:允许字母,数字和空格(至少包含一个字母或数字)

[英]Regular Expression: Allow letters, numbers, and spaces (with at least one letter or number)

I'm currently using this regex ^[A-Z0-9 _]*$ to accept letters, numbers, spaces and underscores. 我目前正在使用这个正则表达式^[A-Z0-9 _]*$来接受字母,数字,空格和下划线。 I need to modify it to require at least one number or letter somewhere in the string. 我需要修改它以在字符串中的某处需要至少一个数字或字母。 Any help would be appreciated! 任何帮助,将不胜感激!

This would be for validating usernames for my website. 这将用于验证我的网站的用户名。 I'd actually like to support as many characters as I can, but just want to ensure that I prevent code injection and that characters will display fine for all users. 我实际上想支持尽可能多的字符,但只是想确保我阻止代码注入,并且字符对所有用户都显示正常。 So I'm definately open to regex validation suggestions that would support a wider set of characters. 因此,我肯定对正则表达式验证建议持开放态度,这些建议可以支持更广泛的字符集。

You simply need to specify your current RE, followed by a letter/number followed by your current RE again: 您只需指定当前的RE,然后再输入一个字母/数字,然后再指定当前的RE:

^[A-Z0-9 _]*[A-Z0-9][A-Z0-9 _]*$

Since you've now stated they're Javascript REs, there's a useful site here where you can test the RE against input data. 既然你现在已经声明它们是Javascript RE,那么这里有一个有用的站点,你可以根据输入数据测试RE。

If you want lowercase letters as well: 如果你想要小写字母:

^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$

To go ahead and get a point out there, instead of repeatedly using these: 要继续并在那里得到一个观点,而不是重复使用这些:

[A-Za-z0-9 _]
[A-Za-z0-9]

I have two (hopefully better) replacements for those two: 我有两个(希望更好)替换这两个:

[\w ]
[^\W_]

The first one matches any word character (alphanumeric and _ , as well as Unicode) and the space. 第一个匹配任何单词字符(字母数字和_ ,以及Unicode)和空格。 The second matches anything that isn't a non-word character or an underscore (alphanumeric only, as well as Unicode). 第二匹配任何不是一个非文字字符或一个下划线(字母数字只,以及为Unicode)。

If you don't want Unicode matching, then stick with the other answers. 如果您不想要Unicode匹配,那么请坚持使用其他答案。 But these just look easier on the eyes (in my opinion). 但这些看起来更容易(在我看来)。 Taking the "preferred" answer as of this writing and using the shorter regexes gives us: 在撰写本文时使用“首选”答案并使用较短的正则表达式给出了我们:

^[\w ]*[^\W_][\w ]*$

Perhaps more readable, perhaps less. 也许更具可读性,也许更少。 Certainly shorter. 当然更短。 Your choice. 你的选择。

EDIT: 编辑:

Just as a note, I am assuming Perl-style regexes here. 就像一个注释,我在这里假设Perl风格的正则表达式。 Your regex engine may or may not support things like \\w and \\W. 你的正则表达式引擎可能支持也可能不支持\\ w和\\ W之类的东西。

EDIT 2: 编辑2:

Tested mine with the JS regex tester that someone linked to and some basic examples worked fine. 使用JS regex测试程序测试了我的链接和一些基本示例正常工作。 Didn't do anything extensive, just wanted to make sure that \\w and \\W worked fine in JS. 没有做任何广泛的事情,只是想确保\\ w和\\ W在JS中运行良好。

EDIT 3: 编辑3:

Having tried to test some Unicode with the JS regex tester site, I've discovered the problem: that page uses ISO instead of Unicode. 尝试使用JS regex测试站点测试一些Unicode后,我发现了问题:该页面使用的是ISO而不是Unicode。 No wonder my Japanese input didn't match. 难怪我的日语输入不匹配。 Oh well, that shouldn't be difficult to fix: 哦,这应该不难解决:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Or so. 或者。 I don't know what should be done as far as JavaScript, but I'm sure it's not hard. 我不知道在JavaScript方面应该做些什么,但我确信这并不难。

^[ _]*[A-Z0-9][A-Z0-9 _]*$

You can optionally have some spaces or underscores up front, then you need one letter or number, and then an arbitrary number of numbers, letters, spaces or underscores after that. 您可以选择在前面放置一些空格或下划线,然后您需要一个字母或数字,然后需要任意数量的数字,字母,空格或下划线。

Something that contains only spaces and underscores will fail the [A-Z0-9] portion. 只包含空格和下划线的东西将使[A-Z0-9]部分失效。

You can use a lookaround : 您可以使用环视

^(?=.*[A-Za-z0-9])[A-Za-z0-9 _]*$

It will check ahead that the string has a letter or number, if it does it will check that the rest of the chars meet your requirements. 它将提前检查字符串是否有字母或数字,如果是,它将检查其余的字符是否符合您的要求。 This can probably be improved upon, but it seems to work with my tests. 这可能会有所改进,但它似乎适用于我的测试。

UPDATE: 更新:

Adding modifications suggested by Chris Lutz : 添加Chris Lutz建议的修改

^(?=.*[^\W_])[\w ]*$/

对我来说@“^ [\\ w] + $”正在工作,允许数字,字母和空格,但需要输入至少一个字母或数字。

This will validate against special characters and leading and trailing spaces: 这将验证特殊字符以及前导和尾随空格:

var strString = "Your String";

strString.match(/^[A-Za-z0-9][A-Za-z0-9 ]\*[A-Za-z0-9]\*$/)
$("#ValuationName").bind("keypress", function (event) {
    if (event.charCode!=0) {
        var regex = new RegExp("^[a-zA-Z ]+$");
        var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
        if (!regex.test(key)) {
            event.preventDefault();
            return false;
        }
    }
});

Simply u can add this to jquery.validationEngine-en.js file 您可以将此添加到jquery.validationEngine-en.js文件中

    "onlyLetterNumberSp": {
                "regex": ^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$,
                "alertText": "* No special characters allowed"
          },

and call it in text field as 并在文本字段中将其命名为

<input type="text" class="form-control validate[required,custom[onlyLetterNumberSp]]"  id="title" name="title" placeholder="Title"/>

暂无
暂无

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

相关问题 正则表达式:允许字母,数字和空格(至少有一个字母不是数字) - Regular Expression: Allow letters, numbers, and spaces (with at least one letter not number) javascript:使用RegEx允许字母,数字和空格(至少包含一个字母和数字) - javascript: Use RegEx to allow letters, numbers, and spaces (with at least one letter and number) 正则表达式强制在字符串中至少包含一个字母和数字? - regular expression to force to have at least one letter and number in a string? 正则表达式:仅限字母、数字、空格、连字符、句点和下划线,并且必须以字母开头 - Regular Expression: Letters, numbers, spaces, hyphens, periods, and underscores only and must begin with a letter 正则表达式至少匹配一个大写字母和至少一个数字和任意数量的特殊字符 - Regular expression to match at least One capital letter and at least One digit and any number of special charecter 至少一个数的正则表达式 - Regular Expression For At Least One Number 需要 4-12 个字母数字字符或特殊字符 (~!@$%&amp;*_+) 至少一个字母的正则表达式,不能有空格 - Need regular expression for 4-12 alphanumeric characters or special characters (~!@$%&*_+) at least one letter, no spaces Javascript正则表达式,表示“至少X个字符,带有大写字母和数字” - Javascript regular expression for “at least X characters, have a capital letter and a number” 密码的正则表达式至少包含 2 个大写字母、2 个小写字母、2 个符号和 2 个任意顺序的数字 - Regular expression for a password containing at least 2 uppercase letters, 2 lowercase letters, 2 symbols, and 2 numbers in any sequence 具有一个连字符(1-5)的数字和具有单个空格的数字(1 2 3 4 5)的正则表达式 - Regular expression for numbers with one hyphen(1-5) and numbers with single spaces (1 2 3 4 5)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM