emotrend

3ta

Feb 7th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. function solve() {
  2. let inputArr = JSON.parse(document.getElementById('arr').value);
  3. let resultSpan = document.getElementById('result');
  4. let wordToSearchFor = inputArr[0];
  5. inputArr.shift();
  6.  
  7. let pattern = /( |^)([A-Za-z]+)\s+(([A-Z!#$%]){8,})( |\.|,|$)/gm;
  8. let matches = [];
  9.  
  10. inputArr.forEach(el => {
  11. let m;
  12.  
  13. if ((m = pattern.exec(el)) !== null) {
  14. let currMatch = (m[0].trim().toLowerCase().split(' '))
  15. if (currMatch[0] === wordToSearchFor.toLowerCase()) {
  16. let currWordInEl = currMatch[1];
  17. let replacingWord = m[3];
  18. matches.push(el.replace(replacingWord, decode(currWordInEl)));
  19. }
  20. } else {
  21. matches.push(el);
  22. }
  23.  
  24. });
  25.  
  26. matches.forEach(e => appendChildEl(e));
  27.  
  28. function decode(el) {
  29. el = el.replace(/!/g, '1');
  30. el = el.replace(/%/g, '2');
  31. el = el.replace(/#/g, '3');
  32. el = el.replace(/\$/g, '4');
  33. return el.toLowerCase();
  34. }
  35.  
  36. function appendChildEl(el) {
  37. let tempP = document.createElement('p');
  38. tempP.textContent = el;
  39. resultSpan.appendChild(tempP);
  40. }
  41.  
  42. //console.log(matches);
  43. }
Add Comment
Please, Sign In to add comment