Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *1. Code:*
- replaceString(txt, keyword){
- let returnTxt = txt;
- if (keyword) {
- const parts = txt.split(keyword);
- const partLen = parts.length;
- const result = [];
- for (let i = 0; i < partLen; i += 1) {
- result.push(parts[i]);
- if (partLen - i > 1)
- result.push(<span className="highlight-txt">{keyword}</span>);
- }
- returnTxt = result;
- }
- return returnTxt;
- }
- *2. Example:*
- const text = "the lazy fox jumps over the quick dog";
- var stringJXS = replaceString(text, 'over');
- // 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