-
Notifications
You must be signed in to change notification settings - Fork 634
/
Copy pathregexp.html
48 lines (39 loc) · 1.4 KB
/
regexp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
<html>
<head>
<title>Regular-Expression in JS</title>
</head>
<script>
// ---------------
// Using search()
// ---------------
var str = 'Information Technology & Management';
console.log('Using search(): '+ str.search(/Technology/i)); // This method searches a string for a specified value and returns the position of the match
// -----------------
// Using replace()
// -----------------
console.log('Using replace(): '+'12-30-45'.replace(/-/g, ':')); // 12:30:45
// ---------------
// Using match()
// ---------------
var re = /\w+\s/g;
var str = 'Information Technology & Management';
var arr = str.match(re); // if the match found, return the match as an array
console.log('Using match(): '+arr);
// --------------
// Using exec()
// --------------
// It searches a string for a specified pattern, and returns the found text as an object.
console.log('Using exec(): '+/Technology/.exec('Information Technology & Management'));
// ---------------
// Using test()
// ---------------
var str = 'Regular Expression in JavaScript';
var patt = new RegExp('in');
var res = patt.test(str); // This method returns true if it finds a match, otherwise it returns false.
console.log('Text found in string? '+ res);
</script>
<body>
Regular Expression in JavaScript
</body>
</html>