Actions

Using regular expressions/zh-cn: Difference between revisions

From LimeSurvey Manual

No edit summary
No edit summary
(40 intermediate revisions by the same user not shown)
Line 178: Line 178:
<source lang="html">/^(?:\(?(?:\+?61|0)4\)?(?:[ -]?[0-9]){7}[0-9]$/</source>
<source lang="html">/^(?:\(?(?:\+?61|0)4\)?(?:[ -]?[0-9]){7}[0-9]$/</source>


==Belgian phone number==
==比利时电话号码==
  <source lang="html">/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/</source>
  <source lang="html">/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/</source>
==Belgian mobile phone number==
==比利时手机号码==
  <source lang="html">/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/</source>
  <source lang="html">/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/</source>
==French phone number==
==法国电话号码==
  <source lang="html">/^((\+|00)33\s?|0)[1-59](\s?\d{2}){4}$/</source>
  <source lang="html">/^((\+|00)33\s?|0)[1-59](\s?\d{2}){4}$/</source>
==French mobile phone number==
==法国移动电话号码==
  <source lang="html">/^((\+|00)33\s?|0)[67](\s?\d{2}){4}$/</source>
  <source lang="html">/^((\+|00)33\s?|0)[67](\s?\d{2}){4}$/</source>
==Luxemburg phone number==
==卢森堡电话号码==
  <source lang="html">/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/</source>
  <source lang="html">/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/</source>
==Luxemburg mobile phone number==
==卢森堡移动电话号码==
  <source lang="html">/^((\+|00\s?)352)?\s?6[269]1(\s?\d{3}){2}$/</source>
  <source lang="html">/^((\+|00\s?)352)?\s?6[269]1(\s?\d{3}){2}$/</source>
=German marks (with optional plus or minus)=
=德国标记(带有可选的正负号)=
  <source lang="html">/^[1-6]{1}[\+|\-]?$/</source>
  <source lang="html">/^[1-6]{1}[\+|\-]?$/</source>


Line 200: Line 200:
  <source lang="html">/([2-9][0-9])/</source>
  <source lang="html">/([2-9][0-9])/</source>


Example: Age 18-35
示例:年龄 18-35
  <source lang="html">/(1[8-9]|2[0-9]|3[0-5])/</source>
  <source lang="html">/(1[8-9]|2[0-9]|3[0-5])/</source>


Example: Age 19-65
示例:年龄 19-65
  <source lang="html">/^(1[8-9]|[2-5][0-9]|6[0-5])$/</source>
  <source lang="html">/^(1[8-9]|[2-5][0-9]|6[0-5])$/</source>




=Number validation=  
=数字验证=  




==Numbers from 1 to 99999==
==1 99999 的数字==
  <source lang="html">/^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/</source>
  <source lang="html">/^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/</source>
  <source lang="html">/^[1-9][0-9]{0,4}$/ does the same as above but should run a little faster</source>
  <source lang="html">/^[1-9][0-9]{0,4}$/ does the same as above but should run a little faster</source>
==Numbers from 1 to 999, 1.000 to 999.999 to 999.999.999==
==1 999, 从1.000 999.999 999.999.999 的数字==
  <source lang="html">/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,2}$/</source>
  <source lang="html">/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,2}$/</source>
Accepts numbers from 1 to 999, 1.000 to 999.999 to 999.999.999 but
匹配从 1 999, 1.000 999.999 999.999.999 的数字,不匹配像 999.1.1 , 94.22.22, 999.1.22, 999.11.1, 999.1.333 的数字。
rejects numbers like 999.1.1 , 94.22.22, 999.1.22, 999.11.1, 999.1.333


==Number validation with optional decimal (for price)==
==带有可选小数的数字验证(价格)==
Accepts numbers from 0 to 199, with 2 decimal optional:
匹配从 0 199 的数字, 可选是否带两位小数:
  <source lang="html">/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/</source>
  <source lang="html">/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/</source>


==Number validation with thousand separator==
==用千位分隔符进行数字验证==
Space as separator, no minus
空格做分隔符,没有减号
  <source lang="html">/^(?!0)\d{1,3}(\ \d{3})*$/</source>
  <source lang="html">/^(?!0)\d{1,3}(\ \d{3})*$/</source>
Dot as separator, minus allowed
点做分隔符,允许减号
  <source lang="html">/^-?(?!0)\d{1,3}(\.\d{3})*$/</source>
  <source lang="html">/^-?(?!0)\d{1,3}(\.\d{3})*$/</source>


It forces two decimal points and accepts numbers from 1.00 to 999,999,999.00 with an optional comma delimiting thousands/millions
强制两位小数,匹配从 1.00 999,999,999.00 的数字,并可用逗号分隔千万/百万,以下数字都是匹配的: 1.00, 1,000.00, 12,345.67, 12345,02, 123,456,468.00, 1234566.00, 123456789.00
including all of the following: 1.00, 1,000.00, 12,345.67, 12345,02, 123,456,468.00, 1234566.00, 123456789.00
but not 1,23.00, 12,3.4 or 1234,43.04
but not 1,23.00, 12,3.4 or 1234,43.04
  <source lang="html">/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/</source>
  <source lang="html">/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/</source>
   
   
Similar to the above: Forces two decimal points but accepts a "0" before decimal separator ",".
和上面相似:强制两位小数,但可以在小数点分隔符 "," 前面加 "0"
  <source lang="html">/[0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/</source>
  <source lang="html">/[0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/</source>


Same as above, but the two decimal points are optional:
和上面一样,但两位小数变成可选项:
<source lang="html">/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/ </source>
<source lang="html">/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/ </source>


==Month (1-12)==
==月份 (1-12)==


If you want to ask for the month a person was born you can validate the input as follows:
如果你要验证某人输入的出生月份是否正确可以使用下面的表达式:


  <source lang="html">/^[0]*[1-9]$|^[0]*1[0-2]$/</source>
  <source lang="html">/^[0]*[1-9]$|^[0]*1[0-2]$/</source>


=Minimum width (set to 3 in this example)=
=最小宽度 (本示例中设置为 3 )=




Line 251: Line 249:




=Currency=
=货币=




==US currency (dollar sign and cents optional)==
==美元(美元符号和美分可选)==


  <source lang="html">/^\$?\d+(\.(\d{2}))?$/</source>
  <source lang="html">/^\$?\d+(\.(\d{2}))?$/</source>


Check for comma usage:
检查逗号用法:
<source lang="html">/^\$?\d{1,3}(\d+(?!,))?(,\d{3})*(\.\d{2})?$/</source>
<source lang="html">/^\$?\d{1,3}(\d+(?!,))?(,\d{3})*(\.\d{2})?$/</source>


==Swiss price==
==瑞士价格==


A number with two decimal numbers after the decimal point of which the last one is either a 5 or a 0:
一个带有两个十进制数字的数字,小数点后的最后一个数字为5或0:


  <source lang="html">/^(\d+)(\.\d(05)?)?$/</source>
  <source lang="html">/^(\d+)(\.\d(05)?)?$/</source>




=Validate score=
=验证分数=




Line 280: Line 278:




=Text validation=
=验证文本=




The [[Question type - Multiple short text|multiple short text question type]] doesn't support minimum or maximum answers at the moment. One way around this is to use a [[Question type - Long free text|long free text question type]] with a regular expression.
[[Question type - Multiple short text|多重短文本题型]] 当前不支持最小或最大的答案。可以使用带正则表达式的 [[Question type - Long free text| 文本输入(长)题型]] 来替代。


The below expression tests for at least one word per line for at least 3 lines and no more than 10 lines:
下面的表达式测试的内容是每行至少一个词,最少3行,最多10行。


  <source lang="html">/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){2,10}/is</source>
  <source lang="html">/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){2,10}/is</source>


If you want, say five words per line, you could change the first and last star/asterisk to {4,}:
如果你要If you want, 在一行里放5个词,你可以把第一个和最后一个星号改成 {4,}:


<source lang="html">/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})){2,10}/is</source>
<source lang="html">/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})){2,10}/is</source>


If you wanted one or more words per line (between line 1 and line 5), you can change the content located within the last curly braces to '''0,4''' (note you use 0 because you're already matching the first line).
如果你要一行里有一个或多个词 (在第一行和第五行之间), 你可以把最后的大括号里的内容改成 '''0,4''' (注意你使用 0 是因为你要从第一行开始匹配).


  <source lang="html">/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){0,4}/is</source>
  <source lang="html">/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){0,4}/is</source>


==Word count==
==单词计数==


  The following restricts the number of words allowed to a minimum of 1 and a maximum of 200:
  以下内容将允许的字数限制为最少1个和最多200个:


<source lang="html">/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/</source>
<source lang="html">/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/</source>


  To increase the minimum change the zero part of {0,199}.
  要增加最小值,请更改{0,199}的零部分。


To increase or decrease the maximum change the "199" part of {0,199}.
要增加或减少最大值,请更改{0,199}的“ 199”部分。


=Time validation=
=时间验证=




There are a number of ways to write time formats. Some of the possible options are 12 hour or 24 hour, with seconds or without. Although it is an option to use the [[Question type - Date|date question type]] (it can also capture time) you can use the [[Question type - Short free text|short free text question type]] with one of the below validation regular expressions.
时间格式有大量的书写方式。一些可能的选项是12小时或24小时,有或没有秒。尽管可以使用 [[Question type - Date|日期题型]] (可以获取时间) ,你可以使用带下面任一个正则表达式的 [[Question type - Short free text|文本输入(短)题型 ]]


The following three validation strings test for 24 hour time (in order of appearances) without seconds, with optional seconds lastly with seconds required:
下面三个验证时间的字符串分别是24小时的时间(按出现顺序) 没有秒,有可选秒和最后必须有秒:


<source lang="html">/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/</source>
<source lang="html">/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/</source>
Line 320: Line 318:
<source lang="html">/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/</source>
<source lang="html">/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/</source>


The following three match 12 hour time, as above with seconds, optional seconds and with seconds required:
下面三个匹配 12 小时时间,和上面一样,有秒,可选秒和必须有秒:


<source lang="html">/^(?">00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))$/</source>
<source lang="html">/^(?">00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))$/</source>
Line 328: Line 326:
<source lang="html">/^(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))$/</source>
<source lang="html">/^(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))$/</source>


The following three match either 12 or 24 hour time as above with seconds, optional seconds and with seconds required:
下面三个匹配 12 或是 24 小时时间,有秒,可选秒和必须有秒:


<source lang="html">/^(?:(?:00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9])$/</source>
<source lang="html">/^(?:(?:00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9])$/</source>
Line 336: Line 334:
<source lang="html">/^(?:(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/</source>
<source lang="html">/^(?:(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/</source>


=US states=
=美国各州=




To validate for one state use the following (example is Texas):
使用下面的表达式验证州 (示例是 Texas):
*TX uppercase only: <source lang="html">/^(TX)$/</source>
*TX 仅大写: <source lang="html">/^(TX)$/</source>
*tx lowercase only: <source lang="html">/^(tx)$/</source>
*tx 仅小写: <source lang="html">/^(tx)$/</source>
*TX upper or lowercase: <source lang="html">/^([T|t][X|x])$/</source>
*TX 大写或小写: <source lang="html">/^([T|t][X|x])$/</source>




=Profanity Filter=
=敏感词过滤器=




To filter profanity words from an answer:
从答案中过滤敏感词:


  <source lang="html">/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/</source>
  <source lang="html">/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/</source>


Replace "ENTERPROFANITYHERE" with your bad word.
"ENTERPROFANITYHERE" 替换你的敏感词。


The \b will allow passing of words such as "assassination" and "hello" if you enter "ass" or "hell" as your profanity word. This also works if you are trying to omit other words, names etc. from answers.
如果你把 "ass" 或是 "hell" 定义为你的敏感词,那么 \b 将允许跳过类似 "assassination" "hello" 的单词,如果你要忽略答案中其他的单词,名字什么的,也可以这么使用。




=Helpful links=
=有帮助的链接=




In the beginning of this wiki section, we recommend you to use https://regex101.com/ to test/create regular expressions. You can also use https://www.regextester.com/ to create expressions in case you are unhappy about the first option.
在这个百科部分的开头部分,我们推荐你使用 https://regex101.com/ 测试和创建正则表达式。你也可以使用 https://www.regextester.com/ 来创建正则表达式,如果你不喜欢第一个选项的话。

Revision as of 10:30, 21 October 2020

正则表达式必须以斜杠("/")作为开头和结尾。你可以在 http://www.regexlib.net/ 找到一个很好的参考示例。如果都有斜杠包围,那么这些模板都可以工作。

你可以通过 正则表达式测试器来测试你的正则表达式。

为了完善百科的这一部分,我们诚挚的建议你将你测试过的成功的正则表达式加入进来,以便使新的 LimeSurvey (潜在的) 用户能够更好的理解这一功能。
  Attention : 请注意下面所有的例子都是在一行内完成!



重要:条件中的正则表达式

请注意在条件编辑器中使用正则表达式,不要包含前后的斜杠。


邮件地址验证

(2018/12 更新): 从这些日子开始,域名不仅可以包含特殊字符,像 “ü” ,还可以使用超过3个字符的名字作为域名,像 tech 或是 company,下面的邮件地址正则表达式只检查输入的邮件地址里是否包含“@” 和 “.” 。

/^.+?@.+?\..+$/

这个过时的正则表达式中,域名被限制在3个字符以内,不能覆盖所有的可用域名:

/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/

邮政编码

澳大利亚的邮政编码

/^[0-9]{4}/

巴西的邮政编码

/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/

加拿大的邮政编码

/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/

美国邮政编码

/^[0-9]{5}([- /]?[0-9]{4})?$/

以零开头的邮政编码,使用:

/^[0-9]{5}(?:-[0-9]{4})?$/

英国邮政编码

/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i

注意这个并不是非常准确,一个精确的验证会非常复杂。比如: 堆栈溢出的答案维基百科 有更多相关的信息。

法国邮政编码

/(^[0-8]\d\d{3}$)|(^9[0-5]\d{3}$)|(^97[1-6]\d{2}$)|(^98[46-8]\d{2}$)/

上面的表达式非常精确,可以用来验证法国的部门是否存在 (前两个数字), 包含海外的部门和海外属地(DOM-TOM).


电话号码

美国电话号码

/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/

或者

/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/

第二个选项将匹配所有的包括非数字标识的加拿大和美国的电话号码,包括:

 . ( ) - (space)

这个将允许你匹配类型下面的电话号码:

  • (555)555 5555
  • 555.555.5555
  • 555 555 5555
  • (555)-555-5555
  • 555-555-5555
  • 555555555

澳大利亚电话号码

下面的模式匹配了所有各种各样的澳大利亚的移动电话和座机的号码,还包括有 "+61" 国家号码前缀的:

  • (02) 9123 6535
  • 03 1234-5345
  • 0412 345 678
  • +61 2 3456 789

但不包括:

  • 234 3450 234
  • a234 534 3432
  • 134567
  • 123456789013

括号,空格和横线都会被忽略掉。

这里的'非常精确'的意思:此处罗列的版本和电话的前4个或前5个数字匹配,以确保这段号码在澳大利亚的有效的。

'不很精确:' 只和前面第一或第二位的数字匹配,因此会允许输入许多无效的数字。

所有澳大利亚电话号码 (移动电话和座机 - 需要区号)

非常精确:

/^\(?(?:\+?61|0)(?:(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}|4\)?[ -]?(?:(?:[01][ -]?[0-9]|2[ -]?[0-57-9]|3[ -]?[1-9]|4[ -]?[7-9]|5[ -]?[018])[ -]?[0-9]|3[ -]?0[ -]?[0-5])(?:[ -]?[0-9]){5})$/

不很精确:

/^(?:\+?61|0)[2-478](?:[ -]?[0-9]){8}$/

所有澳大利亚电话号码 (移动电话和座机 - 需要区号)

非常精确:

/^\(?(?:\+?61|0)(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}$/

不很精确:

/^(?:\+?61|\(?0)[2378]\)?(?:[ -]?[0-9]){8}$/

新南威尔士州的固定电话(区号可选)

非常精确:

/^(?:\(?(?:\+?61|0)2\)?[ -]?)?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])(?:[ -]?[0-9]){6}$/

不很精确:

/^(?:\(?(?:\+?61|0)2\)?(?:[ -]?[0-9]){7}[0-9]$/

维多利亚和塔斯马尼亚州的固定电话(区号可选)

非常精确:

/^(?:\(?(?:\+?61|0)3\)?[ -]?)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])(?:[ -]?[0-9]){6}$/

不很精确:

/^(?:\(?(?:\+?61|0)3\)?(?:[ -]?[0-9]){7}[0-9]$/

昆士兰固定电话号码(区号可选)

非常精确:

/^(?:\(?(?:\+?61|0)7\)?[ -]?)?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)(?:[ -]?[0-9]){6}$/

不很精确:

/^(?:\(?(?:\+?61|0)7\)?(?:[ -]?[0-9]){7}[0-9]$/

南澳大利亚,北领地,西澳大利亚的座机电话号码(区号可选)

非常精确:

/^(?:\(?(?:\+?61|0)8\)?[ -]?)?(?:5[1-4]|6[0-8]|[7-9][0-9])$/

不很精确:

/^(?:\(?(?:\+?61|0)8\)?(?:[ -]?[0-9]){7}[0-9]$/

仅限澳大利亚手机号码

非常精确:

/^(?:\+?61|0)4 ?(?:(?:[01] ?[0-9]|2 ?[0-57-9]|3 ?[1-9]|4 ?[7-9]|5 ?[018]) ?[0-9]|3 ?0 ?[0-5])(?: ?[0-9]){5}$/

不很精确:

/^(?:\(?(?:\+?61|0)4\)?(?:[ -]?[0-9]){7}[0-9]$/

比利时电话号码

/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/

比利时手机号码

/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/

法国电话号码

/^((\+|00)33\s?|0)[1-59](\s?\d{2}){4}$/

法国移动电话号码

/^((\+|00)33\s?|0)[67](\s?\d{2}){4}$/

卢森堡电话号码

/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/

卢森堡移动电话号码

/^((\+|00\s?)352)?\s?6[269]1(\s?\d{3}){2}$/

德国标记(带有可选的正负号)

/^[1-6]{1}[\+|\-]?$/


年龄验证

示例:年龄 20-99

/([2-9][0-9])/

示例:年龄 18-35

/(1[8-9]|2[0-9]|3[0-5])/

示例:年龄 19-65

/^(1[8-9]|[2-5][0-9]|6[0-5])$/


数字验证

从 1 到 99999 的数字

/^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/
/^[1-9][0-9]{0,4}$/ does the same as above but should run a little faster

从 1 到 999, 从1.000 到 999.999 到 999.999.999 的数字

/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,2}$/

匹配从 1 到 999, 1.000 到 999.999 到 999.999.999 的数字,不匹配像 999.1.1 , 94.22.22, 999.1.22, 999.11.1, 999.1.333 的数字。

带有可选小数的数字验证(价格)

匹配从 0 到 199 的数字, 可选是否带两位小数:

/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/

用千位分隔符进行数字验证

空格做分隔符,没有减号

/^(?!0)\d{1,3}(\ \d{3})*$/

点做分隔符,允许减号

/^-?(?!0)\d{1,3}(\.\d{3})*$/

强制两位小数,匹配从 1.00 到 999,999,999.00 的数字,并可用逗号分隔千万/百万,以下数字都是匹配的: 1.00, 1,000.00, 12,345.67, 12345,02, 123,456,468.00, 1234566.00, 123456789.00 but not 1,23.00, 12,3.4 or 1234,43.04

/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/

和上面相似:强制两位小数,但可以在小数点分隔符 "," 前面加 "0" 。

/[0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/

和上面一样,但两位小数变成可选项:

/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/

月份 (1-12)

如果你要验证某人输入的出生月份是否正确可以使用下面的表达式:

/^[0]*[1-9]$|^[0]*1[0-2]$/

最小宽度 (本示例中设置为 3 )

/^.{3,}$/


货币

美元(美元符号和美分可选)

/^\$?\d+(\.(\d{2}))?$/

检查逗号用法:

/^\$?\d{1,3}(\d+(?!,))?(,\d{3})*(\.\d{2})?$/

瑞士价格

一个带有两个十进制数字的数字,小数点后的最后一个数字为5或0:

/^(\d+)(\.\d(05)?)?$/


验证分数

1-10

/^[1-9]{1}$|^10$/

1-100

/^[1-9]?[0-9]{1}$|^100$/


验证文本

多重短文本题型 当前不支持最小或最大的答案。可以使用带正则表达式的 文本输入(长)题型 来替代。

下面的表达式测试的内容是每行至少一个词,最少3行,最多10行。

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){2,10}/is

如果你要If you want, 在一行里放5个词,你可以把第一个和最后一个星号改成 {4,}:

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})){2,10}/is

如果你要一行里有一个或多个词 (在第一行和第五行之间), 你可以把最后的大括号里的内容改成 0,4 (注意你使用 0 是因为你要从第一行开始匹配).

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){0,4}/is

单词计数

以下内容将允许的字数限制为最少1个和最多200个:
/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/
要增加最小值,请更改{0,199}的零部分。

要增加或减少最大值,请更改{0,199}的“ 199”部分。

时间验证

时间格式有大量的书写方式。一些可能的选项是12小时或24小时,有或没有秒。尽管可以使用 日期题型 (可以获取时间) ,你可以使用带下面任一个正则表达式的 文本输入(短)题型

下面三个验证时间的字符串分别是24小时的时间(按出现顺序) 没有秒,有可选秒和最后必须有秒:

/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/
/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/
/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/

下面三个匹配 12 小时时间,和上面一样,有秒,可选秒和必须有秒:

/^(?">00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))$/
/^(?:00:[0-5][0-9](?::[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?::[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?::[0-5][0-9])? (?:pm|PM))$/
/^(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))$/

下面三个匹配 12 或是 24 小时时间,有秒,可选秒和必须有秒:

/^(?:(?:00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9])$/
/^(?:(?:00:[0-5][0-9](?[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?[0-5][0-9])? (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9](?[0-5][0-9])?)$/
/^(?:(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/

美国各州

使用下面的表达式验证州 (示例是 Texas):

  • TX 仅大写:
    /^(TX)$/
    
  • tx 仅小写:
    /^(tx)$/
    
  • TX 大写或小写:
    /^([T|t][X|x])$/
    


敏感词过滤器

从答案中过滤敏感词:

/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/

用 "ENTERPROFANITYHERE" 替换你的敏感词。

如果你把 "ass" 或是 "hell" 定义为你的敏感词,那么 \b 将允许跳过类似 "assassination" 和 "hello" 的单词,如果你要忽略答案中其他的单词,名字什么的,也可以这么使用。


有帮助的链接

在这个百科部分的开头部分,我们推荐你使用 https://regex101.com/ 测试和创建正则表达式。你也可以使用 https://www.regextester.com/ 来创建正则表达式,如果你不喜欢第一个选项的话。