简体   繁体   English

JavaScript正则表达式,不匹配数字,空格和选定的符号

[英]JavaScript regular expressions to match no digits, whitespace and selected symbols

Thanks for taking a look. 谢谢参观。

My goal is to come up with a regexp that will match input that contains no digits, whitespace or the symbols !@£$%^&*()+= or any other symbol I may choose. 我的目标是提出一个正则表达式,它将匹配不包含数字,空格或符号的输入!@£$%^&*()+=或我可能选择的任何其他符号。

I am however struggling to grasp precisely how regular expressions work. 然而,我正在努力精确地掌握正则表达式的工作原理。

I started out with the simple pattern /\\D/ , which from my understanding will match the first non-digit character it can find. 我开始使用简单模式/\\D/ ,根据我的理解,它将匹配它可以找到的第一个非数字字符。 This would match the string 'James' which is correct but also 'James1' which I don't want. 这将匹配正确的字符串'James'以及我不想要的'James1'。

So, my understanding is that if I want to ensure that a pattern is not found anywhere in a given string, I use the ^ and $ characters, as in /^\\D$/ . 所以,我的理解是,如果我想确保在给定字符串中的任何地方都找不到模式,我会使用^$字符,如/^\\D$/ Now because this will only match a single character that is not a digit, I needed to use + to specify that 1 or more digits should not be founds in the entire string, giving me the expression /^\\D+$/ . 现在因为这只会匹配一个不是数字的单个字符,我需要使用+来指定不应该在整个字符串中找到一个或多个数字,给我表达式/^\\D+$/ Brilliant, it no longer matches 'James1'. 很棒,它不再匹配'James1'。

Question 1 问题1

Is my reasoning up to this point correct? 我的理由是否正确?

The next requirement was to ensure no whitespace is in the given string. 下一个要求是确保给定字符串中没有空格。 \\s will match a single whitespace and [^\\s] will match the first non-whitespace character. \\s将匹配单个空格, [^\\s]将匹配第一个非空白字符。 So, from my understanding I just had to add this to what I have already to match strings that contain no digits and no whitespace. 所以,根据我的理解,我只需将其添加到我已经匹配的字符串中,这些字符串不包含数字,也没有空格。 Again, because [^\\s] will only match a single non-white space character, I used + to match one or more whitespace characters, giving the new regexp of /^\\D+[^\\s]+$/ . 同样,因为[^\\s]只匹配一个非空格字符,我用+来匹配一个或多个空白字符,给出新的正则表达式为/^\\D+[^\\s]+$/

This is where I got lost, as the expression now matches 'James1' or even 'James Smith25'. 这是我迷路的地方,因为表达现在匹配'James1'甚至'James Smith25'。 What? 什么? Massively confused at this point. 此时大肆混淆。

Question 2 问题2

Why is /^\\D+[^\\s]+$/ matching strings that contain spaces? 为什么/^\\D+[^\\s]+$/匹配包含空格的字符串?

Question 3 问题3

How would I go about writing the regular expression I'm trying to solve? 我将如何编写正在尝试解决的正则表达式?

While I am keen to solve the problem I am more interested in figuring where my understanding of regular expressions is lacking, so any explanations would be helpful. 虽然我很想解决这个问题,但我更感兴趣的是找出我对正则表达式的理解缺乏的地方,所以任何解释都会有所帮助。

  1. Not quite; 不完全的; ^ and $ are actually "anchors" - they mean "start" and "end", it's actually a little more complicated, but you can consider them to mean the start and end of a line for now - look up the various modifiers on regular expressions if you're interested in learning more about this. ^$实际上是“锚点” - 它们的意思是“开始”和“结束”,它实际上有点复杂,但你现在可以认为它们意味着一条线的起点和终点 - 在常规上查找各种修饰符表达式如果您有兴趣了解更多相关信息。 Unfortunately ^ has an overloaded meaning; 不幸的是^有一个超载的意思; if used inside square brackets it means "not", which is the meaning you are already acquainted with. 如果在方括号内使用它意味着“不”,这就是你已经熟悉的含义。 It's very important that you understand the difference between these two meanings and that the definition in your head actually applies only to character range matching! 理解这两个含义之间的区别并且头脑中的定义实际上仅适用于字符范围匹配非常重要!

    Contributing further to your confusion is that \\d means "a numerical digit" and \\D means "not a numerical digit". 让您感到困惑的是, \\ d表示“数字数字”, \\ D表示“不是数字数字”。 Similarly \\s means "a whitespace (space/tab/newline/etc.) character" and \\S means "not a whitespace character." 类似地, \\ s表示“空格(空格/制表符/换行符等)字符”, \\ S表示“不是空白字符”。

    It's worth noting that \\d is effectively a shortcut for [0-9] (note that - has a special meaning inside square brackets), and \\D is a shortcut for [^0-9] . 值得注意的是\\ d实际上是[0-9]的快捷方式(请注意-方括号内有特殊含义), \\ D[^ 0-9]的快捷方式。

  2. The reason it's matching strings that contain spaces is that you've asked for "1+ non-numerical digits followed by 1+ non-space characters" - so it'll match lots of strings! 它匹配包含空格的字符串的原因是你要求“1+非数字后跟 1个非空格字符” - 所以它会匹配很多字符串! I think that perhaps you don't understand that regular expressions match bits of strings, you're not adding constraints as you go, but rather building up bots of matchers that will match bits of corresponding strings. 我想也许你不明白正则表达式匹配字符串的位数,你不是随意添加约束,而是建立匹配相应字符串位的匹配器。

  3. /^[^\\d\\s!@£$%^&*()+=]+$/ is the answer you're looking for - I'd look at it like this: /^[^\\d\\s!@£$%^&*()+=]+$/是您正在寻找的答案 - 我会这样看:

    i. 一世。 [] - match a range of characters [] - 匹配一系列字符

    ii. II。 []+ - match one or more of that range of characters []+ - 匹配该字符范围中的一个或多个

    iii. III。 [^\\d\\s]+ - match one or more characters that do not match \\d (numerical digit) or \\s (whitespace) [^\\d\\s]+ - 匹配一个或多个与\\ d (数字数字)或\\ s (空格) 匹配字符

    iv. IV。 [^\\d\\s!@£$%^&*()+=]+ - here's a bunch of other characters I don't want you to match [^\\d\\s!@£$%^&*()+=]+ - 这里有一堆我不想让你匹配的其他角色

    v. ^[^\\d\\s!@£$%^&*()+=]+$ - now there are anchors applied, so this matcher has to apply to the whole line otherwise it fails to match v。 ^[^\\d\\s!@£$%^&*()+=]+$ - 现在应用了锚点,所以这个匹配器必须应用于整行,否则无法匹配

A useful website to explore regexs is http://regexr.com/3b9h7 - which I supply with my suggested solution as an example. 一个有用的网站来探索正则表达式是http://regexr.com/3b9h7 - 我提供了我建议的解决方案作为一个例子。 Edit: Pruthvi Raj's link to debuggerx is awesome! 编辑:Pruthvi Raj与debuggerx的链接很棒!

Just insert every character you don't want to include in a negated character class as follows: 只需插入您不希望包含在否定字符类中的每个字符,如下所示:

^[^\s\d!@£$%^&*()+=]*$

DEMO DEMO

正则表达式可视化

Debuggex Demo Debuggex演示

^ - start of the string
[^...] - matches one character that is not in `...`
\s - matches a whitespace (space, newline,tab)
\d - matches a digit from 0 to 9
* - a quantifier that repeats immediately preceeding element by 0 or more times

so the regex matches any string that has 所以正则表达式匹配任何具有的字符串

1. string that has a beginning
2. containing 0 or more number of characters that is not whitesapce, digit, and all the symbols included in the character class ( In this example !@£$%^&*()+=) i.e., characters that are not included in the character class `[...]`
3.that has ending

NOTE: 注意:

If the symbols you don't want it to have also includes - , a hyphen, don't put it in between some other characters because it is a metacharacter in character class, put it at last of character class 如果你不想让它的符号也包括- ,一个连字符,不要把它放在其他一些字符之间,因为它是字符类中的元字符,把它放在字符类的最后

Is my reasoning up to this point correct? 我的理由是否正确?

Almost. 几乎。 /\\D/ matches any character other than a digit, but not just the first one (if you use g option). /\\D/匹配除数字之外的任何字符,但不仅仅匹配第一个字符(如果使用g选项)。

and [^\\s] will match the first non-whitespace character 和[^ \\ s]将匹配第一个非空白字符

Almost, [^\\s] will match any non-whitespace character, not just the first one (if you use g option). 几乎, [^\\s]将匹配任何非空白字符,而不仅仅是第一个(如果使用g选项)。

/^\\D+[^\\s]+$/ matching strings that contain spaces? / ^ \\ D + [^ \\ s] + $ /匹配包含空格的字符串?

Yes, it does, because \\D matches a space (space is not a digit). 是的,它确实如此,因为\\D匹配一个空格(空格不是数字)。

Why is /^\\D+[^\\s]+$/ matching strings that contain spaces? 为什么/ ^ \\ D + [^ \\ s] + $ /匹配包含空格的字符串?

Because \\D+ in /^\\D+[^\\s]+$/ can match spaces. 因为\\D+ in /^\\D+[^\\s]+$/可以匹配空格。

Conclusion : 结论

Use 采用

^[^\d\s!@£$%^&*()+=]+$

It will match strings that have no digits and spaces, and the symbols you do not allow . 它将匹配没有数字和空格的字符串,以及您不允许的符号

Mind that to match a literal - , ] or [ with a character class, you either need to escape them, or use at the start or end of the expression. 请注意 ,要匹配文字-][与字符类,您需要转义它们,或在表达式的开头或结尾使用。 To play it safe, escape them. 为了安全起见,逃避它们。

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

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