Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. *1. Code:*
  2.  
  3. replaceString(txt, keyword){
  4. let returnTxt = txt;
  5.  
  6. if (keyword) {
  7. const parts = txt.split(keyword);
  8. const partLen = parts.length;
  9. const result = [];
  10.  
  11. for (let i = 0; i < partLen; i += 1) {
  12. result.push(parts[i]);
  13.  
  14. if (partLen - i > 1)
  15. result.push(<span className="highlight-txt">{keyword}</span>);
  16. }
  17. returnTxt = result;
  18. }
  19.  
  20. return returnTxt;
  21. }
  22.  
  23. *2. Example:*
  24. const text = "the lazy fox jumps over the quick dog";
  25. var stringJXS = replaceString(text, 'over');
  26.  
  27. // result equipvalent to: "the lazy fox jumps <span className="highlight-txt">over</span> the quick dog"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement