JavaScript RegExp
Reference
v A
regular expression is a pattern of characters.
v The
pattern is used for searching and replacing characters in
strings.
v The RegExp
Object is a regular expression with added Properties and Methods.
Syntax
let a = /hello/;
or
let a = new RegExp('hello');
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<h2 style="background-color:
rgb(9, 238, 9);color:rgb(255, 0, 0);font-weight: bolder;font-family: Georgia,
'Times New Roman', Times, serif;">Regular Expression</h2>
<h5 style="color:
rgb(231, 15, 166);">Output</h5>
</head>
<body>
<script>
let a = /hello/;
let b = a.test('hello
Daisy');
console.log(b);
document.write(b);
let c = /hi/;
let d = c.test('hello
Daisy');
console.log(d);
document.write("<br>");
document.write(d);
</script>
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<h2 style="background-color:
rgb(9, 238, 9);color:rgb(255, 0, 0);font-weight: bolder;font-family: Georgia,
'Times New Roman', Times, serif;">Regular Expression</h2>
<h5 style="color:
rgb(231, 15, 166);">Output</h5>
</head>
<body>
<script>
let message = 'Hi, are you there? hi, HI...';
let re = /hi/gi;
let matches = [];
let match;
do {
match = re.exec(message);
if(match)
{
matches.push(match);
}
} while(match != null)
console.dir(matches);
</script>
</body>
</html>
Output
Replacing strings
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<h2 style="background-color:
rgb(9, 238, 9);color:rgb(255, 0, 0);font-weight: bolder;font-family: Georgia,
'Times New Roman', Times, serif;">Regular Expression</h2>
<h5 style="color:
rgb(231, 15, 166);">Output</h5>
</head>
<body>
<script>
let a = "Hi hello how are you";
let result = a.replace(/you/g,'U');
document.write(result);
</script>
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
input,div{
font-size: 2em;
}
div
{
color:red;
}
</style>
<script>
function callfor()
{
var txt=document.getElementById("mystr").value;
document.getElementById("out").innerHTML=txt.split('
');
console.log(txt.split('
'));
}
function takewords()
{
var txt=document.getElementById("mystr").value;
var pat=/a/g;
var x=txt.match(pat);
document.getElementById("out").innerHTML=x;
}
function takenum()
{
var txt=document.getElementById("mystr").value;
var pt=/[0-9]/g;
var x=txt.match(pt);
document.getElementById("out").innerHTML=x;
}
function begintext()
{
var txt=document.getElementById("mystr").value;
var pt=/[0-9]/g;
var x=txt.match(pt);
document.getElementById("out").innerHTML=x;
}
function takewordsalone()
{
var txt=document.getElementById("mystr").value;
//var pt=/[a-zA-Z]/g;
// var pt=/[^a-z]+/gi;
var pt=/[a-z]{3}/gi;
var x=txt.match(pt);
document.getElementById("out").innerHTML=x;
}
function endtext()
{
var txt=document.getElementById("mystr").value;
//var p1=/[0-9]+[a]$/gi
var x=txt.match(p1);
document.getElementById("out").innerHTML=x;
}
</script>
</head>
<body>
<input type="text" id="mystr">
<button onclick="callfor()">Check</button>
<button onclick="takewords()">Words</button>
<button onclick="takenum()">Numbers </button>
<button onclick="takewordsalone()">Words </button>
<button onclick="begintext()">Begin </button>
<button onclick="endtext()">End </button>
<div id="out"></div>
</body>
</html>
Output
0 comments:
Post a Comment