There are three methods of the String Object that uses Regular Expressions as input.
stringObj.match(rgExp)
stringObj.match(rgExp)match executes a search on a string using a Regular Expression pattern, and returns an array containing the results of that search.
If the match method does not find a match, it returns null.
If it finds a match, match returns an array. If the global flag (g) is not set, element zero of the array contains the entire match, while elements 1 – n contain any sub matches that have occurred within the match. If the global flag is set, elements 0 - n contain all matches that occurred.
stringObj.replace(rgExp, replaceText)
stringObj.replace(rgExp, replaceText)replace returns a copy of a string with rgExp replaced with replaceText. The string stringObj is not modified by the replace method.
rgExp can be an instance of a Regular Expression object or a String object or literal. If rgExp is not an instance of a Regular Expression object, it is converted to a string, and an exact search is made for the results; no attempt is made to convert the string into a Regular Expression.
replaceText is a String object or string literal containing the text to replace for every successful match of rgExp in stringObj. It can also be a function that returns the replacement text.
Returned from the replace method is a copy of stringObj after the specified replacements have been made.
stringObj.search(rgExp)
stringObj.search(rgExp)search returns the position of the first substring match in a Regular Expression search using the Regular Expression object rgExp.
The search method indicates if a match is present or not. If a match is found, search returns an integer value that indicates the index from where the match occurred. If no match is found, it returns -1.
Using Regular Expression to Replace Commas With Line Breaks
If you want to send an email and in the email text list the answers to a multi question from the survey, you can use categoryLabels. Converted into a string this will give the items separated by commas. If you want to have the answers on one line each instead of separated by commas you have to replace the commas with line breaks. If the email is sent as plain text, you then have to replace the commas with the special character \n.
This script will replace the commas in a listing of the answers given on a multi question brands:
var body : String = "";
body += "Here are the answers on the brands question:\r\n\r\n"
var brandsAnswers = f("brands").categoryLabels();
brandsAnswers = brandsAnswers.toString();
brandsAnswers = brandsAnswers.replace(/,/g,"\r\n");
body += brandsAnswers
SendMail("interviewer@confirmit.com",f("email"),"Answers",body);
If the mail is sent as HTML, you have to replace the commas with the HTML <br> tag instead:
var body : String = "";
body += "Here are the answers on the brands question:<br><br>"
var brandsAnswers = f("brands").categoryLabels();
brandsAnswers = brandsAnswers.toString();
brandsAnswers = brandsAnswers.replace(/,/g,"<br>");
body += brandsAnswers
SendMail("interviewer@confirmit.com",f("email"),"Answers",body,"","",0,0);
Post Codes in the United Kingdom
Post codes in the UK can be in the following formats:
LN NLL
LLN NLL
LNN NLL
LLNN NLL
LLNL NLL
LNL NLL
where L is a letter and N is a number. A post code is one or two letters, followed by either one number and an optional letter or by two numbers,AND then a space and a number and two letters.
A validation script for post codes for the UK could be as shown below, with the open text question postalcode:
var pcode : String = f("postalcode").get();
var re = /^((?:(?:[a-z]{1,2}\d{1,2}|[a-z]{2}\d[a-z]|[a-z]\d[a-z]) \d[a-z]{2}))$/i;
if(pcode.search(re) == -1)
{
RaiseError();
SetQuestionErrorMessage(LangIDs.en,"Please provide a post code using a valid format.");
}We allow both upper and lower case letters (i). The first half of the valid formats are covered by (?:(?:[a-z]{1,2}\d{1,2}|[a-z]{2}\d[a-z]|[a-z]\d[a-z]). Within this, there are three possibilities for the first part before the blank: The first being a combination of 1-2 letters and 1-2 digits (LN, LLN, LNN or LLNN), the second being 2 letters, 1 digit and 1 letter (LLNL), and the third being 1 letter, 1 digit and 1 letter (LNL). The end of the string must always be NLL which is taken care of by the sub expression \d[a-z]{2}))$.
Note that not all letters are valid in all positions, so this regular expression will accept some invalid postal codes, but it will at least help the respondent to comply with the basic format of the postal codes.
Make a script to validate a US zip code. Question ID: zipcode. US zip codes are 5 digit codes. So it is tempting to set up a numeric question with total digits 5, and let the default validation do the work. However, the first number can be a zero (0), and to make sure that it is not removed when the value is stored (as it would for numeric questions), the question should be set up as an open text question with field width 5 instead of as a numeric. The easiest way to validate that all 5 characters are digits, is to use a Regular Expression.
The answer is given in APPENDIX A Answers to Exercises.