Created
November 28, 2017 19:25
-
-
Save imparvez/9fe65c4d3a653ff97051b9bdfaeb7739 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// RegEx | |
// Sample: /expression/.test('string'); | |
// 1. expression: is any regular expression that we build | |
// 2. string: it is a string that we put under test | |
// 3. test: this method returns true or false | |
// / & /: mark the start and end of the expression | |
// expression between / are literals. They are treated as literals characters. | |
// Start with single character | |
/a/.test("a"); //true | |
/a/.test("b"); // false | |
// Find a string which has a particular character for eg: a | |
/a/.test("abc"); // true | |
/a/.test("bcd"); // false | |
/a/.test("cba"); // true | |
var e = /a/; | |
e.test("abc"); // true | |
e.test("bcd"); // false | |
e.test("cba"); // true | |
// Reach Multi Character | |
/ab/.test("abacus"); // true | |
/bac/.test("abacus"); // true | |
/abc/.test("abacus"); // false (No pattern of abc was found) | |
/abas/.test("abacus"); // false (No pattern of abas was found) | |
// Patterns in Number | |
var b = /0|1|2|3|4|5|6|7|8|9/; // | (pipe) means OR | |
b.test("42"); // true | |
b.test("The answer is 42"); // true => Shouldn't be true. | |
var c = /[0123456789]/; // 9 pipes were removed with two square brackets | |
c.test("42"); // true | |
c.test("The number is 42"); // true | |
var d = /[0-9]/; | |
d.test("42"); // true | |
d.test("The number is 42"); // true | |
// The prefix and suffix patterns | |
// PREFIX: any character that follow ^ should be at the start of the string. | |
/^a/.test("abc"); // true => a is in the start of the string | |
/^a/.test("bca"); // false => a is not in the start of the string | |
/^http/.test("https://pineboat.in"); // true | |
/^http/.test("ftp://pineboat.in"); // false | |
// SUFFIX: this pattern follows $ at the end of the pattern to be test directs the test to look for end of the string | |
// Look for string that ends with js | |
/js$/.test("regex.js"); // true | |
/js$/.test("regex.sj"); // false | |
var f = /^[0-9]$/; | |
f.test("42"); // false | |
f.test("The answer is 42"); // false | |
f.test("7"); // true | |
f.test(7); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment