Advertisement
Guest User

regex

a guest
Dec 19th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3. let n = Number(input[0]);
  4.  
  5. for(let i = 1; i <= n; i++) {
  6.  
  7.     let line = input[i];
  8.  
  9.     let regex = /!([A-Z][a-z]{3,})!:\[([A-Za-z]{3,})\]/g;
  10.  
  11.     let isValid = regex.test(line);
  12.    
  13.  
  14.     if(isValid) {      
  15.        
  16.         let match = regex.exec(line);
  17.  
  18.         let command = match[1];
  19.         let message = match[2];        
  20.  
  21.         console.log(`Command: ${command} Message: ${message}`);
  22.  
  23.     } else {
  24.         console.log('The message is invalid');
  25.     }
  26.  
  27. }
  28.    
  29. }
  30.  
  31. solve([
  32.     '3',
  33.     '!play!:[TheNewSong]',
  34.     '!Ride!:[Bike]',
  35.     '!Watch!:[LordofTheRings]'
  36.   ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement