Regular Expression - Quick JavaScript Interview Questions (2015)

Quick JavaScript Interview Questions (2015)

Chapter 7. Regular Expression

Q. What is Regular Expression in JavaScript?
ANSWER
Regular expressions are used for pattern matching in a string. A regular expression in JavaScript can be declared in two ways. Syntax for these two approaches is listed below.

var aRegExpression = new RegExp(pattern,modifiers); var aRegExpression = /pattern/modifiers;
The details of these syntaxes are as follows:
pattern : it represents the search string.
modifiers : it represents the conditional switches that will be applied to the pattern matching

Q. What are the methods of a regular expression?
ANSWER
There are 3 different methods for regular expressions are listed below.

exec() : scans for a matching string and returns the first matched string
test() : scans for a matching string and returns boolean value false or true.

toString() : This method returns the string value of a regular expression.

Q. What is the switch \w represents?
ANSWER
The \w represents an alphanumeric character. The following code demonstrate the use of \w switch .

var regEx= /\wu/; var out1 = regEx.exec("sandeep Ku Patel"), out2 = regEx.test("sandeep Ku Patel");

console.log(out1);
console.log(out2);

Below screenshot shows the output of the above code which pattern match any substring having alphanumeric character with 2nd character as u.

Q. Write a Regular expression to reverse the first and last name? For example Sandeep Patel becomes Patel Sandeep.
ANSWER
To reverse first and last name we have to use replace() function on input string with regular expression. The following code shows the method to reverse first and last name.

var reverseFirstLastName = function(nameString){
var regex = /(\w+)\s(\w+)/,
resultString = nameString.replace(regex, '$2 $1');
return resultString;
};

var inputName = "Sandeep Patel",
output = reverseFirstLastName(inputName);
console.log(output);
The following screenshot shows the chrome console executing above code and output is printed on console.

Q. Write a Regular expression to validate email address? ANSWER
To validate email we have to use test() method on input string which returns true or false on execution. The following code shows the method to validate email string.

function validateEmail(email) {
var re =
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\

9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
};

var email1 = "sandeeppateltech@gmail.com",
email2 = "sandeep@@gmail.com",
result1 = validateEmail(email1),
result2 = validateEmail(email2);

console.log(result1);
The following screenshot shows the output of the email validation code in a chrome developer console.

Q. Write a Regular expression to validate a URL ?
ANSWER
For validating URL we have considered the protocol will be either http/https/ftp and it must start with WWW word .Considering these points the javascript code for validating a URL is as follows.

function validateURL(url) {
var regExUrl = new RegExp("(http|ftp|https)://[\w-]+(\.[\w
]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?"); return regExUrl.test(url);
};
var url1 = "http://www.google.com",
url2 = "htttp://www.google.com",
result1 = validateURL(url1),
result2 = validateURL(url2);
console.log(result2);

The following screenshot shows the chrome console with URL validation code in execution for a given URL.