Skip to content

Commit

Permalink
adding example for regex and evaluation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chadsmith12 committed May 17, 2018
1 parent 58b205e commit 430467d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
44 changes: 44 additions & 0 deletions regex-evaluating functions/app.js
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);
13 changes: 13 additions & 0 deletions regex-evaluating functions/index.html
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>

0 comments on commit 430467d

Please sign in to comment.