0% found this document useful (0 votes)
11 views

Regular ExpressionExamples

regular exp

Uploaded by

Disha Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Regular ExpressionExamples

regular exp

Uploaded by

Disha Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Regular Expression Examples

10/12/2023 Prepared By Mrs S.S.Gawai 1


<!DOCTYPE html>
<html>
<body>

<h1>JavaScript String Methods</h1>


<p>Search a string for "W3Schools", and display the position of the match:</p>

<p id="demo"></p>

<script>
let text = "Visit W3Schools!";
let n = text.search("W3Schools");
document.getElementById("demo").innerHTML = n;
</script>

</body>
</html>

10/12/2023 Prepared By Mrs S.S.Gawai 2


<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Regular Expressions</h2>

<p>Search a string for "w3Schools", and display the position of the


match:</p>

<p id="demo"></p>

<script>
let text = "Visit W3Schools!";
let n = text.search(/w3Schools/i);
document.getElementById("demo").innerHTML = n;
</script>

</body>
</html>

10/12/2023 Prepared By Mrs S.S.Gawai 3


<!DOCTYPE html>
<html>

<body>

<h1>JavaScript String Methods</h1>


<p>Replace "Microsoft" with "W3Schools" in the paragraph below:</p>

<button onclick="myFunction()">Try it</button>

<p id="demo">Please visit Microsoft!</p>

<script>
function myFunction() {
let text = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML =
text.replace("Microsoft","W3Schools");
}
</script>

</body>
10/12/2023 Prepared By Mrs S.S.Gawai 4
</html>
<!DOCTYPE html>
<html>

<body>

<h2>JavaScript String Methods</h2>

<p>Replace "Microsoft" with "W3Schools" in the paragraph below:</p>

<button onclick="myFunction()">Try it</button>

<p id="demo">Please visit Microsoft!</p>

<script>
function myFunction() {
let text = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML =
text.replace(/microsoft/i, "W3Schools");
}
</script>

</body>
</html>
10/12/2023 Prepared By Mrs S.S.Gawai 5
Describe regular expression. Explain search () method used in
regular expression with suitable example.

Regular Expression: A regular expression is very


similar to a mathematical expression, except a
regular expression tells the browser how to
manipulate text rather than numbers by using
special symbols as operators. Search() method:
str.search() method takes a regular
expression/pattern as argument and search for the
specified regular expression in the string. This
method returns the index where the match found.

10/12/2023 Prepared By Mrs S.S.Gawai 6


<html>
<body>
<script>
function myFunction() {

// input string
var str = "Good Morning!";

// searching string with modifier i


var n = str.search(/Morning/i);

document.write(n + '<br>');

// searching string without modifier i


var n = str.search(/Morning/);

document.write(n);
}
myFunction();
</script>
</body>
10/12/2023 Prepared By Mrs S.S.Gawai 7
</html>
<html> <body> <h3>Using the <i> Regular expression </i> to
validate email in JavaScript </h3> <div id = "output"> </div>
<button onclick = "validateEmail()"> Validate any email
</button>
<script>var output = document.getElementById('output');
function validateEmail() {
let userEmail = prompt("Enter your email.", "[email protected]");
let regex = /^[a-z0-9]+@[a-z]+\.[a-z]{2,3}$/;
let result = regex.test(userEmail);

10/12/2023 Prepared By Mrs S.S.Gawai 8


if (result) {
output.innerHTML = "The " + userEmail + " is a
valid email address!"; } else { output.innerHTML =
"The " + userEmail + " is not a valid email
address!"; } } </script> </body> </html>

10/12/2023 Prepared By Mrs S.S.Gawai 9


<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Regular Expressions</h2>

<p>The test() method returns true if it finds a match,


otherwise false.</p>

<p>Search a string for the character "e":</p>

<p id="demo"></p>

<script>
let text = "The best things in life are free";
let pattern = /e/;
let result = pattern.test(text);

document.getElementById("demo").innerHTML = result;
</script>
</body>
10/12/2023 Prepared By Mrs S.S.Gawai 10
</html>
<!DOCTYPE html>
<html>
<body>
<h1>Display a Telephone Input Field</h1>
<form action="/action_page.php">
<label for="phone">Enter a phone number:</label><br><br>
<input type="tel" id="phone" name="phone" placeholder="123-45-678"
pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}" required><br><br>
<small>Format: 123-45-678</small><br><br>
<input type="submit">
</form>
</body>
</html>
10/12/2023 Prepared By Mrs S.S.Gawai 11
10/12/2023 Prepared By Mrs S.S.Gawai 12
List ways of protecting your web page and
describe any one of them

Ways of protecting Web Page:


1)Hiding your source code
2)Disabling the right MouseButton
3) Hiding JavaScript 4) Concealing
E-mail address.

10/12/2023 Prepared By Mrs S.S.Gawai 13


1) Hiding your source code The source code for your web page—
including your JavaScript—is stored in the cache, the part of
computer memory where the browser stores web pages that were
requested by the visitor. A sophisticated visitor can access the cache
and thereby gain access to the web page source code. However, you
can place obstacles in the way of a potential peeker. First, you can
disable use of the right mouse button on your site so the visitor can't
access the View Source menu option on the context menu. This hides
both your HTML code and your JavaScript from the visitor.
Nevertheless, the visitor can still use the View menu's Source option
to display your source code. In addition, you can store your
JavaScript on your web server instead of building it into your web
page. The browser calls the JavaScript from the web server when it is
needed by your web page. Using this method, the JavaScript isn't
visible to the visitor, even if the visitor views the source code for the
web page.

10/12/2023 Prepared By Mrs S.S.Gawai 14


2)Disabling the right MouseButton
The following example shows you how to disable the
visitor's right mouse button
while the browser displays your web page. All the action
occurs in the JavaScript
that is defined in the <head> tag of the web page.
The JavaScript begins by defining the BreakInDetected()
function. This function
is called any time the visitor clicks the right mouse button
while the web page is
displayed. It displays a security violation message in a dialog
box whenever a
visitor clicks the right mouse button
The BreakInDetected() function is called if the visitor clicks
any button other
than the left mouse button.

10/12/2023 Prepared By Mrs S.S.Gawai 15


3) Hiding JavaScript You can hide your JavaScript from a
visitor by storing it in an external fi le on your web
server. The external fi le should have the .js fi le
extension. The browser then calls the external file
whenever the browser encounters a JavaScript
element in the web page. If you look at the source
code for the web page, you'll see reference to the
external .js fi le, but you won't see the source code for
the JavaScript. The next example shows how to create
and use an external JavaScript file. First you must tell
the browser that the content of the JavaScript is
located in an external file on the web server rather
than built into the web page. You do this by assigning
the fi le name that contains the JavaScripts to the src
attribute of the

10/12/2023 Prepared By Mrs S.S.Gawai 16


4) Concealing E-mail address: Many of us have endured spam
at some point and have probably blamed every merchant we
ever patronized for selling our e-mail address to spammers.
While e-mail addresses are commodities, it's likely that we
ourselves are the culprits who invited spammers to steal our
e-mail addresses. Here's what happens: Some spammers
create programs called bots that surf the Net looking for e-
mail addresses that are embedded into web pages, such as
those placed there by developers to enable visitors to contact
them. The bots then strip these e-mail addresses from the
web page and store them for use in a spam attack. This
technique places developers between a rock and a hard
place. If they place their e-mail addresses on the web page,
they might get slammed by spammers.

10/12/2023 Prepared By Mrs S.S.Gawai 17


If they don't display their e-mail addresses, visitors will not be able to
get in touch with the developers. The solution to this common problem
is to conceal your e-mail address in the source code of your web page
so that bots can't fi nd it but so that it still appears on the web page.
Typically, bots identify e-mail addresses in two ways: by the mailto:
attribute that tells the browser the e-mail address to use when the
visitor wants to respond to the web page, and by the @ sign that is
required of all e-mail addresses. Your job is to confuse the bots by
using a JavaScript to generate the e-mail address dynamically.
However, you'll still need to conceal the e-mail address in your
JavaScript, unless the JavaScript is contained in an external JavaScript
file, because a bot can easily recognize the mailto: attribute and the @
sign in a JavaScript

10/12/2023 Prepared By Mrs S.S.Gawai 18


<html>
<head>
<title>Conceal Email Address</title>
<script>
function CreateEmailAddress(){
var x = manish*c_o_m'
var y = 'mai'
var z = 'lto'
var s = '?subject=Customer Inquiry'
x = x.replace('&','@')
x = x.replace('*','.')
x = x.replace('_','')
x = x.replace('_','')
var b = y + z +':'+ x + s
window.location=b
}
-->
</script>
</head>
<body>
<input type="button" value="Help"
onclick="CreateEmailAddress()">
</body>
</html>
10/12/2023 Prepared By Mrs S.S.Gawai 19
10/12/2023 Prepared By Mrs S.S.Gawai 20
10/12/2023 Prepared By Mrs S.S.Gawai 21

You might also like