Todorov_Stanimir

patternVsRegex

Jul 14th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. test = (text) => {
  2.     let pattern = /[_]{1,}/g;
  3.     let result1 = [];
  4.     let result2 = [];
  5.  
  6.     while ((word = pattern.exec(text)) !== null) {
  7.         result1.push(word[0]);
  8.     }
  9.     while ((word = new RegExp("[_]{1,}", "g").exec(text)) !== null) {
  10.         if (result2.length <= result1.length) result2.push(word[0]); else break;
  11.     }
  12.  
  13.     result1.forEach((word, index) => {
  14.         console.log(`${word.length} vs ${result2[index].length}`)
  15.     });
  16. }
  17. test('Hi, grandma! I\'m so ____ to write to you. ______ the winter vacation, so _______ things happened. My dad bought me a sled. Mom started a new job as a __________. My brother\'s ankle is ________, and now it bothers me even more. Every night Mom cooks ___ on your recipe because it is the most delicious. I hope this year Santa will _____ me a robot.')
  18.  
  19. // Result
  20. // 4 vs 4
  21. // 6 vs 4
  22. // 7 vs 4
  23. // 10 vs 4
  24. // 8 vs 4
  25. // 3 vs 4
  26. // 5 vs 4
Advertisement
Add Comment
Please, Sign In to add comment