Regular ExpressionExamples
Regular ExpressionExamples
<p id="demo"></p>
<script>
let text = "Visit W3Schools!";
let n = text.search("W3Schools");
document.getElementById("demo").innerHTML = n;
</script>
</body>
</html>
<p id="demo"></p>
<script>
let text = "Visit W3Schools!";
let n = text.search(/w3Schools/i);
document.getElementById("demo").innerHTML = n;
</script>
</body>
</html>
<body>
<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>
<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.
// input string
var str = "Good Morning!";
document.write(n + '<br>');
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);
<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