javascript 三个字母和两个数字的正则表达式字符串,带有前后空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3534523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:25:53  来源:igfitidea点击:

RegEx string for three letters and two numbers with pre- and post- spaces

javascriptregex

提问by Mark Byers

Two quick questions:

两个快速问题:

  1. What would be a RegEx string for three letters and two numbers with space before and after them (i.e. " LET 12 ")?
  2. Would you happen to know any good RegEx resources/tools?
  1. 三个字母和两个数字前后有空格的 RegEx 字符串是什么(即“ LET 12 ”)?
  2. 你会碰巧知道任何好的 RegEx 资源/工具吗?

回答by Mark Byers

For a good resource, try this websiteand the program RegexBuddy. You may even be able to figure out the answer to your question yourself using these sites.

对于一个好的资源,试试这个网站和程序RegexBuddy。您甚至可以使用这些网站自己找出问题的答案。

To start you off you want something like this:

要开始你,你需要这样的东西:

/^[a-zA-Z]{3}\s+[0-9]{2}$/

But the exact details depend on your requirements. It's probably a better idea that you learn how to use regular expressions yourself and then write the regular expression instead of just copying the answers here. The small details make a big difference. Examples:

但具体细节取决于您的要求。最好自己学习如何使用正则表达式,然后编写正则表达式,而不是在此处复制答案。小细节带来大不同。例子:

  • What is a "letter"? Just A-Z or also foreign letters? What about lower case?
  • What is a "number"? Just 0-9 or also foreign numerals? Only integers? Only positive integers? Can there be leading zeros?
  • Should there be a single space between the letters and numbers? Or any amount of any whitespace? Even none?
  • Do you want to search for this string in a larger text? Or match a line exactly?
  • etc..
  • 什么是“信”?只是AZ还是外国字母?小写呢?
  • 什么是“数”?只是 0-9 还是外国数字?只有整数?只有正整数?可以有前导零吗?
  • 字母和数字之间应该有一个空格吗?或者任何数量的任何空格?甚至没有?
  • 您想在较大的文本中搜索此字符串吗?或者完全匹配一条线?
  • 等等..

The answers to these questions will change the regular expression. It would be much faster for you in the long run to learn how to create the regular expression than to completely specify your requirements and wait for other people to reply.

这些问题的答案将改变正则表达式。从长远来看,学习如何创建正则表达式比完全指定您的要求并等待其他人回复要快得多。

I forgot to mention that there will be a space before and after. How do I include that?

忘了说前后会有空格。我如何包含它?

Again you need to consider the questions:

同样,您需要考虑以下问题:

  • Do you mean just one space or any amount of spaces? Possibly not always a space but only sometimes?
  • Do you mean literally a space character or any whitespace characters?
  • 你的意思是只有一个空格还是任意数量的空格?可能不总是一个空间,而只是有时?
  • 你的意思是字面上的空格字符还是任何空格字符?

My guess is:

我的猜测是:

/^\s+[a-zA-Z]{3}\s+[0-9]{2}\s+$/

回答by Daniel Vandersluis

/[a-z]{3} [0-9]{2}/iwill match 3 letters followed by a whitespace character, and then 2 numbers. [a-z]is a character classcontaining the letters a through z, and the {3}means that you want exactly 3 members of that class. The space character matches a literal space (alternately, you could use \s, which is a "shorthand" character class that matches any whitespace character). The iat the end is a pattern modifierspecifying that your pattern is case-insenstive.

/[a-z]{3} [0-9]{2}/i将匹配 3 个字母,后跟一个空格字符,然后是 2 个数字。[a-z]是包含字母 a 到 z的字符类,这{3}意味着您需要该类的 3 个成员。空格字符匹配文字空格(或者,您可以使用\s,它是匹配任何空白字符的“速记”字符类)。将i在年底是一个模式修改指定您的模式是区分insenstive。

If you want the entire string to only be that, you need to anchorit with ^and $:

如果您希望整个字符串只是那个,您需要使用and锚定它:^$

/^[a-z]{3} [0-9]{2}$/i

Regular expression resources:

正则表达式资源:

回答by Richard Fearn

^([A-Za-z]{3}) ([0-9]{2})$assuming one space between the letters/numbers, as in your example. This will capture the letters and numbers separately.

^([A-Za-z]{3}) ([0-9]{2})$假设字母/数字之间有一个空格,如您的示例所示。这将分别捕获字母和数字。

I use http://gskinner.com/RegExr/- it allows you to build a regex and test it with your own text.

我使用http://gskinner.com/RegExr/- 它允许您构建正则表达式并使用您自己的文本对其进行测试。

回答by John M Gant

As you can probably tell from the wide variety of answers, RegEx is a complex subject with a wide variety of opinions and preferences, and often more than one way of doing things. Here's my preferred solution.

正如您可能从各种各样的答案中看出的那样,RegEx 是一个复杂的主题,具有各种各样的意见和偏好,而且通常做事的方式不止一种。这是我的首选解决方案。

^[a-zA-Z]{3}\s*\d{2}$

I used [a-zA-Z]instead of \wbecause \wsometimes includes underscores.

我使用[a-zA-Z]而不是\w因为\w有时包含下划线。

The \s*is to allow zero or more spaces.

\s*是允许零个或多个空格。

I try to use character classes wherever possible, which is why I went with \d.

我尽量使用字符类,这就是我使用\d.

回答by SMK

try this regularexpression

试试这个正则表达式

[^"\r\n]{3,}

回答by Sudhir Jonathan

\w{3}\s{1}\d{2}And I like this site.

\w{3}\s{1}\d{2}我喜欢这个网站。

EDIT:[a-zA-Z]{3}\s{1}\d{2}- The \wsupports numeric characters too.

编辑:[a-zA-Z]{3}\s{1}\d{2}- 也\w支持数字字符。