-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregExp.js
54 lines (46 loc) · 1.98 KB
/
regExp.js
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
49
50
51
52
53
54
///////////////////////////////////////////////////Introduction to the Regular Expression Challenges
/// us .test() !! use for testing you regexp sentense!!!!!!!!!
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex.test(myString); // Change this line
/////////////////////////////// using "alternation" | "or" | "|"
let petString = "James has a pet cat.";
let petRegex = /dog|cat|bird|fish/; // Change this line
let result = petRegex.test(petString);
/////////////////////////////////////////
let username = "JackOfAllTrades";
let userCheck = /^[a-z]{2,}\d*$/i;
let result = userCheck.test(username);
//////////////////////////////////////////
//w - letters
//\ W - NO letters
//\ s - whitespace
//\ S - NO whitespace
//\ d - digital
// ^ [] - NO getMatchs
// /^ ../ begin string
// /..$/ end string
// /..?../ -i dont now
/////////////////////////////////////////////////////////
//password
let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\D*\d{2})/; // two positive lookheads + min 5 letters + 2 digits
let result = pwRegex.test(sampleWord);
///////////////////////////////////////////////////////Reuse Patterns Using Capture Groups
let repeatStr = "regex regex";
let repeatRegex = /(\w+)\s\1/;
repeatRegex.test(repeatStr); // Returns true - -!!!! for testing youre regExp!!!!!!!!!!! USE!!!!!!!
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]
/////////////////////////////////////////////////////// replace!!
let wrongText = "The sky is silver.";
let silverRegex = /silver/;
wrongText.replace(silverRegex, "blue");
// Returns "The sky is blue."
////////////////////////////////////////////////////// replace using capture group access with "$"
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
// Returns "Camp Code"
//////////////////////////////////////////////remove string whitespaces
let hello = " Hello, World! ";
let myWsRegExp = /^\s+|\s+$/g;
let result = hello.replace(myWsRegExp, ''); ///// ore use .trim();
/////////////////////////////////////////////////