-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path67_regex.js
98 lines (66 loc) · 4.57 KB
/
67_regex.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// A regular expression, also known as a regex, is a pattern that can be used to match or search for strings in JavaScript.It is a powerful tool that can be used to validate input, extract information from strings, and perform complex search and replace operations.
// Here's an example of a simple regular expression in JavaScript:
// const pattern = /hello/;
// const result = pattern.test('hello world');
// console.log(result); // Outputs true
// In this example, we define a regular expression pattern that matches the string "hello".We then use the test() method of the regular expression object to check if the string "hello world" matches the pattern.The test() method returns a boolean value indicating whether the string matches the pattern or not.
// Regular expressions can also include special characters and modifiers to create more complex patterns.Here are some examples:
// Match any digit
// const pattern1 = /\d/;
// const result1 = pattern1.test('123');
// console.log(result1); // Outputs true
// Match any non-digit
// const pattern2 = /\D/;
// const result2 = pattern2.test('abc');
// console.log(result2); // Outputs true
// Match any whitespace character
// const pattern3 = /\s/;
// const result3 = pattern3.test(' ');
// console.log(result3); // Outputs true
// Match any non-whitespace character
// const pattern4 = /\S/;
// const result4 = pattern4.test('abc');
// console.log(result4); // Outputs true
// Match any word character (letter, digit, or underscore)
// const pattern5 = /\w/;
// const result5 = pattern5.test('a1_');
// console.log(result5); // Outputs true
// Match any non-word character
// const pattern6 = /\W/;
// const result6 = pattern6.test('!@#');
// console.log(result6); // Outputs true
// Match a string that starts with "hello" and ends with "world"
// const pattern7 = /^hello.*world$/;
// const result7 = pattern7.test('hello there world');
// console.log(result7); // Outputs true
// In this example, we use various special characters and modifiers to create more complex regular expressions.For example, the \d character matches any digit, while the \D character matches any non - digit.The ^ and $ characters match the start and end of a string, respectively.The.* modifier matches any character any number of times.
// In summary, regular expressions are a powerful tool for matching and searching for strings in JavaScript.They can be used to validate input, extract information from strings, and perform complex search and replace operations.With practice, you can become proficient in using regular expressions to manipulate strings in your JavaScript programs.
// Regular expressions, or regex for short, are a powerful tool for working with text data.They allow you to search, match, and manipulate strings based on certain patterns.
// In JavaScript, you can create a regular expression by enclosing a pattern in forward slashes(/pattern/).For example, the regular expression / hello / would match the string "hello" in a larger piece of text.
// Here are some basic patterns you can use in regular expressions:
// Character Classes:
// [abc] matches any one of the characters a, b, or c
// [a - z] matches any lowercase letter from a to z
// [A - Z] matches any uppercase letter from A to Z
// [0 - 9] matches any digit from 0 to 9
// [^ abc] matches any character that is not a, b, or c
// Quantifiers:
// matches one or more occurrences of the preceding character or group
// matches zero or more occurrences of the preceding character or group
// ? matches zero or one occurrence of the preceding character or group
// { n } matches exactly n occurrences of the preceding character or group
// { n,} matches n or more occurrences of the preceding character or group
// { n, m } matches between n and m occurrences of the preceding character or group
// Anchors:
// ^ matches the beginning of a line
// $ matches the end of a line
// \b matches a word boundary
// Grouping:
// ( ) creates a capturing group
// (?: ) creates a non - capturing group
// Here's an example of how you can use regular expressions in JavaScript:
// const myString = "The quick brown fox jumps over the lazy dog.";
// const pattern = /fox/;
// const result = pattern.test(myString); // true
// In this example, we create a regular expression pattern that matches the word "fox".We then use the test() method to check if this pattern matches the string "The quick brown fox jumps over the lazy dog." The result is true, because the string contains the word "fox".
// I hope this introduction to regular expressions in JavaScript is helpful! Let me know if you have any further questions.