forked from rohan-paul/Awesome-JavaScript-Interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-if-String-has-NO-numbers.js
55 lines (32 loc) · 2.71 KB
/
check-if-String-has-NO-numbers.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
let str1 = "88kl85"
let str2 = "8885"
let str3 = "abc"
let re = /[^0-9]+$/
// console.log(str1.match(re)) // => null
// console.log(str2.match(re)) // => null
// console.log(str3.match(re)) // => [ 'abc', index: 0, input: 'abc' ]
// Return true if str has no number
// SOLUTION-1
ifStringHasNoNumber = str => str.match(re) !== null
console.log(ifStringHasNoNumber(str1)) // => false
console.log(ifStringHasNoNumber(str2)) // => false
console.log(ifStringHasNoNumber(str3)) // true
// SOLUTION-2
let re2 = /[0-9]+$/
// console.log(str1.match(re2)) // => [ '85', index: 4, input: '88kl85' ]
// console.log(str2.match(re2)) // => [ '8885', index: 0, input: '8885' ]
// console.log(str3.match(re2)) // => null
// Return true if str has no number
ifStringHasNoNumber2 = str => str.match(re2) === null
console.log(ifStringHasNoNumber2(str1)) // => false
console.log(ifStringHasNoNumber2(str2)) // => false
console.log(ifStringHasNoNumber2(str3)) // true
/* match() method - match() method retrieves the matches when matching a string against a regular expression.
Its Return value >> If the string matches the expression, it will return an Array containing the **entire matched string** as the first element, followed by any results captured in parentheses. If there were no matches, null is returned. So, from the passed-in string (passed as an argument to match() method) either the whole matched string is returned or null is returned.
See below implementation of match()
^ Matches the position at the beginning of the input string. If the RegExp object's Multiline property is set, ^ also matches the position following '\n' or '\r'. See the opposite functionality of this function - check-if-String-has-only-numbers.js
PAUL VERY IMP - The same '^' char is used for as A negative range characters. Matches any character not in the specified range. For example, '[^a-z]' matches any character not in the range 'a' through 'z'. BUT NOTICE TO USE IT AS A NEGATIVE-RANGE-CHARACTER, I HAVE TO PUT IT INSIDE THE SQUARE BRACKET.
$ >> Matches the position at the end of the input string. If the RegExp object's Multiline property is set, $ also matches the position preceding '\n' or '\r'.
PAUL - So here, by including '^' at the beginning of the input string AND '$' at the end of the input string - I am making sure, the 're' variable takes the whole input string from START-TO-END as the block to match. So when I pass this 're' to match() method - IT WILL MATCH THE ENTIRE INPUT ARGUMENT (i.e. the entire input string passed as argument ) and return null if the entire input argument does not match.
+ >> Matches the preceding character or sub-expression one or more times. For example, 'zo+' matches "zo" and "zoo", but not "z". + is equivalent to {1,}.
*/