espresso_coffee espresso_coffee - 1 year ago 117
Javascript Question

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. i would like the same regular expression in ColdFusion. I have everything working fine but empty string. Here is my JavaScript:

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

and here is what I have in 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. If anyone knows how to match an empty string please let me know. Thanks.

Answer Source

Set the min value to 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.

Pattern details:

  • ^ - start of string
  • [A-Za-z0-9 ]{0,50} - 0 to 50 ASCII letters, digits or spaces
  • $ - end of string.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download