Monday, 11 September 2006

REGEX; pattern matching

regular expression to match a U.S. phone number:
\(?\d{3}\)? ?\d{3}[-.]\d{4}

This regex matches phone numbers like "(314)555-4000". 
Ask yourself if the regex would match "314-555-4000" or "555-4000". 
The answer is no in both cases. Writing this pattern on one line 
conceals both flaws and design decisions. The area code is 
required and the regex fails to account for a separator 
between the area code and prefix.

/  
    \(?     # optional parentheses
      \d{3} # area code required
    \)?     # optional parentheses
    [-\s.]? # separator is either a dash, a space, or a period.
      \d{3} # 3-digit prefix
    [-.]    # another separator
      \d{4} # 4-digit line number
/x


replace all instances of double quotes with 
single - /g option is all instances
replace(/\"/g,'\'')