-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasSpace.js
More file actions
executable file
·73 lines (56 loc) · 1.33 KB
/
hasSpace.js
File metadata and controls
executable file
·73 lines (56 loc) · 1.33 KB
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
// JavaScript - Check If a String Contains any Whitespace Characters
// 1. Using Regular Expressions
{
const s = "Hello world";
const hasSpace = /\s/.test(s);
console.log(hasSpace);
}
// 2. Using String.prototype.includes() Method
{
const s = "Helloworld";
const hasSpace = s.includes(" ");
console.log(hasSpace);
}
// 3. Using String.prototype.match() Method
{
const s = "Hello world";
const hasSpace = s.match(/\s/) !== null;
console.log(hasSpace);
}
// 4. Using String.prototype.search() Method
{
const s = "Hello world";
const hasSpace = s.search(/\s/) !== -1;
console.log(hasSpace);
}
// 5. Using a for Loop
{
const s = "HelloWorld";
let hasSpace = false;
for (const char of s) {
if (/\s/.test(char)) {
hasSpace = true;
break;
}
}
console.log(hasSpace);
}
// 6. Using Array.prototype.some() Method
{
const s = "Hello World";
const hasSpace = [...s].some((char) => /\s/.test(char));
console.log(hasSpace);
}
// 7. Using Array.prototype.filter() Method
{
const s = "HelloWorld";
const hasSpace = s.split("").filter((char) => /\s/.test(char)).length > 0;
console.log(hasSpace);
}
// Using javascript sets
{
const s = "Hello world";
const set = new Set([" ", "\t", "\n", "\r"]);
const hasSpace = [...s].some((char) => set.has(char));
console.log(hasSpace)
}