Client Side Scripting Unit-5 RegEx Pyq's
Client Side Scripting Unit-5 RegEx Pyq's
RegEx
W-19
Q.4 (a) Describe regular expression. Explain search () method used in regular expression with suitable example. (4M)
<html>
<head>
<title>Search method in Regex</title>
<script>
function Search(){
var str="Good Morning!";
var re1=str.search(/Morning/i);
document.write(re1+"<br>");
var re2=str.search(/morning/);
document.write(re2);
}
</script>
</head>
<body>
<button onclick="Search()">Search </button>
</body>
</html>
W-22
Q. Write a JavaScript function to check the first character of a string is uppercase or not.
<html>
<head>
<title>Check 1st alphabet Uppercase or not</title>
<script>
function checkUpperCase(){
var str=prompt("Enter a string ");
var regUpper=/^[A-Z]/;
if(regUpper.test(str)){
alert ("Is in upper case");
}else{
alert ("Not in uper case");
}
}
checkUpperCase();
</script>
</head>
</html>
S-22
Q.2 (d) State what is regular expression. Explain its meaning with the help of a suitable example. (4M)
Q. Write a javascript program to validate email ID of the user using regular expression. (4M)
1
Unit-5 Regular Expressions Vikas Patil
S-23
Q.4 (a) Explain test() and exec() method of Regular Expression object with example. (4M)
Q.5 (c) Write HTML code to design a form that displays textboxes for accepting UserID and Aadhar No. and a SUBMIT
button. UserID should contain 10 alphanumeric characters and must start with Capital Letter. Aadhar No. should contain
12 digits in the format nnnn nnnn nnnn. Write JavaScript code to validate the UserID and Aadhar No. when theuser clicks
on SUBMIT button. (6M)
<html>
<head>
<title>Validate user id and aadhar no</title>
<script>
function submitData(){
var uid=document.getElementById("uid").value;
var regAadhar=/^\d{4}-\d{4}-\d{4}$/;
if(!regId.test(uid)){
}else if(!regAadhar.test(aadhar)){
alert("Invalid Aadhar NO. . It should be in nnnn-nnnn-nnnn format");
}else{
alert("Valid User");
}
</script>
</head>
W-23 <body>
2
Unit-5 Regular Expressions Vikas Patil
if(pattern.test(str))
{
alert("MSBTE found in String");
}
else
{
alert("MSBTE not Found");
}
}
check();
</script>
</body>
</html>
Ans. ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Ans. ^\d{4}-\d{4}-\d{4}$
Ans. ^\(\d{3}\)-\(\d{8}\)$