Day 7: Regular Expressions III Solution
Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 7: Regular Expressions III Solution.
Also visit this link:ย Day 7: Regular Expressions II Solution
Day 7: Regular Expressions III Solution
Task
Complete the function in the editor below by returning aย RegExpย object,ย re, that matchesย every integerย in some stringย s.
Constraints
- The length of stringย sย isย =>3ย .
- Itโs guaranteed that stringย sย containsย at leastย one integer.
Output Format
The function must return aย RegExpย object that matchesย every integerย in some stringย s.
Sample Input 0
102, 1948948 and 1.3 and 4.5
Sample Output 0
102
1948948
1
3
4
5
Explanation 0
When we callย matchย on stringย sย and pass the correctย RegExpย as our argument, it returns the following array of results:ย [ ‘102’, ‘1948948’, ‘1’, ‘3’, ‘4’, ‘5’ ].
Sample Input 1
1 2 3
Sample Output 1
1
2
3
Explanation 1
When we callย matchย on stringย sย and pass the correctย RegExpย as our argument, it returns the following array of results:ย [ ‘1’, ‘2’, ‘3’ ].
Solution โ Day 7: Regular Expressions III
'useย strict'; process.stdin.resume(); process.stdin.setEncoding('utf-8'); letย inputStringย =ย ''; letย currentLineย =ย 0; process.stdin.on('data',ย inputStdinย =>ย { ย ย ย ย inputStringย +=ย inputStdin; }); process.stdin.on('end',ย _ย =>ย { ย ย ย ย inputStringย =ย inputString.trim().split('\n').map(stringย =>ย { ย ย ย ย ย ย ย ย returnย string.trim(); ย ย ย ย }); ย ย ย ย ย ย ย ย main();ย ย ย ย }); functionย readLine()ย { ย ย ย ย returnย inputString[currentLine++]; } functionย regexVar()ย { ย ย ย ย /* ย ย ย ย ย *ย Declareย aย RegExpย objectย variableย namedย 're' ย ย ย ย ย *ย Itย mustย matchย ALLย occurrencesย ofย numbersย inย aย string. ย ย ย ย ย */ ย ย ย ย letย reย =ย /\d+/g; ย ย ย ย /* ย ย ย ย ย *ย Doย notย removeย theย returnย statement ย ย ย ย ย */ ย ย ย ย returnย re; } functionย main()ย { ย ย ย ย constย reย =ย regexVar(); ย ย ย ย constย sย =ย readLine(); ย ย ย ย ย ย ย ย constย rย =ย s.match(re); ย ย ย ย ย ย ย ย forย (constย eย ofย r)ย { ย ย ย ย ย ย ย ย console.log(e); ย ย ย ย } }