简体   繁体   English

如何在正则表达式ColdFusion中允许空字符串?

[英]How to allow empty string in regex ColdFusion?

I have done my regular expression in JavaScript and my regex allows alpha characters, numbers, space and empty string. 我已经用JavaScript完成了正则表达式,而我的正则表达式则允许使用字母字符,数字,空格和空字符串。 i would like the same regular expression in ColdFusion. 我想在ColdFusion中使用相同的正则表达式。 I have everything working fine but empty string. 我一切正常,但字符串为空。 Here is my JavaScript: 这是我的JavaScript:

/^([A-Z0-9 ]{1,50})?$/i

and here is what I have in ColdFusion: 这是我在ColdFusion中拥有的东西:

<cfset match = REMatch("^[A-Za-z0-9 ]{1,50}$",myText)> 

this will match everything I want but empty string. 这将匹配我想要的所有内容,但字符串为空。 I can not use ? 我不能使用? in ColdFusion because that will look to match any character in the string. 在ColdFusion中使用,因为它看起来可以匹配字符串中的任何字符。 If anyone knows how to match an empty string please let me know. 如果有人知道如何匹配一个空字符串,请告诉我。 Thanks. 谢谢。

Set the min value to 0 将最小值设置为0

<cfset match = REMatch("^[A-Za-z0-9 ]{0,50}$",myText)>
                                      ^

The limiting quantifier allows setting both thresholds, min and max. 限制量词允许同时设置最小和最大阈值。 When you set the min value to 0, it will allow matching nothing, empty string. 将最小值设置为0时,将不匹配任何内容(空字符串)。

Pattern details : 图案细节

  • ^ - start of string ^ -字符串开头
  • [A-Za-z0-9 ]{0,50} - 0 to 50 ASCII letters, digits or spaces [A-Za-z0-9 ]{0,50} -0到50个ASCII字母,数字或空格
  • $ - end of string. $ -字符串结尾。

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

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