-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding example for regex and evaluation functions
- Loading branch information
1 parent
58b205e
commit 430467d
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Basic example of regular expressions in javascript. | ||
* This example shows evaluation functions. | ||
*/ | ||
|
||
// literal expression of 'hello' | ||
// each character is a literal character and will be taken at face value. | ||
let re = /hello/; | ||
|
||
// exec - Returns result in an array if there is a match, or null if there is no match. | ||
// gives you what matches, the index it started matching at, and the input string | ||
const result = re.exec('hello world'); | ||
// this wil give null as there is no match. | ||
const result2 = re.exec('hilo'); | ||
|
||
console.log(result); | ||
console.log(result2); | ||
|
||
// test - Returns true or false | ||
// true as hello matches. | ||
const testResult = re.test('hello'); | ||
// can make it case insensitive | ||
re = /hello/i; | ||
const testResult2 = re.test('Hello'); | ||
console.log(testResult); | ||
console.log(testResult2); | ||
|
||
// g makes the regular expression global | ||
re = /hello/g; | ||
|
||
// match - Returns result array or null | ||
const str = 'hello there'; | ||
const matchResult = str.match(re); | ||
console.log(matchResult); | ||
|
||
// search - Returns the index of the first match. If not found, returns -1. | ||
const str2 = 'hello there'; | ||
const searchResult = str2.search(re); | ||
console.log(searchResult); | ||
|
||
// replace - returns new string with some or all matches of a pattern | ||
const replaceStr = 'hello there'; | ||
const newStr = str.replace(re, 'Hi'); | ||
console.log(newStr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Regex SandBox</title> | ||
</head> | ||
<body> | ||
|
||
<script src="app.js"></script> | ||
</body> | ||
</html> |