littlewolf
littlewolf

Reputation: 181

Regex throws error in Coldfusion but works in JS

I have an email regex that I've been using in Javascript and works fine, but when I try to use it in Coldfusion I get an error.

The regex is:

<cfset regex = '^[a-zA-Z0-9.!#\$%&’*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:(\.[a-zA-Z0-9-]+)+)+$'>

The error I get is:

The CFML compiler was processing:

An expression beginning with /', on line 28, column 16.This message is usually caused by a problem in the expressions structure. A cfset tag beginning on line 28, column 2.

It seems to be complaining about the / but I have escaped it \/. Furthermore, as far as I can tell, it is not on column 16. When I add characters before or after the / the line number doesn't change. If I add characters to the variable name, for example, it does.

I have tried with and without escape backslashes, enclosing the regex in /.../ and parens (^....$).

The regex should match whole word a valid email address such as [email protected], [email protected], etc.

It will not match if there are spaces or invalid characters such as: not@[email protected], ' [email protected] ', or wrong.pattern@foo

Does anyone see where my error is?

Upvotes: 1

Views: 93

Answers (1)

Matt Busche
Matt Busche

Reputation: 14333

In ColdFusion you need to escape the #. You can do this by adding consecutive ##

<cfset regex = '^[a-zA-Z0-9.!##\$%&’*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:(\.[a-zA-Z0-9-]+)+)+$'>

Upvotes: 2

Related Questions