0

I have a small problem with replace words in html.

<p id ="demo">Hello, Hello how are you! </p> 

I want final result like this: Hello, how are you!

It should to filter it to remove duplicate items, join them back.

var span = document.getElementById('demo'), text = span.innerHTML.split('').filter(function (allItems,i,p) {	return i == p.indexOf(allItems); }).join(''); span.innerHTML = text;
<p id ="demo">Hello, Hello how are you </p>

4
  • You only want words removed and not duplicate letters, right? Commented Jan 10, 2018 at 17:57
  • yes, right! I only want words removed Commented Jan 10, 2018 at 17:58
  • stackoverflow.com/questions/16843991/… Commented Jan 10, 2018 at 18:05
  • Hello, and Hello are different since there is comma involved. Is it the only string you want to change or there can be many like this ? Commented Jan 10, 2018 at 18:11

2 Answers 2

2

This should work:

var span = document.getElementById('demo'), text = span.innerHTML.split(/(\W+)/).map(function(currValue, i, array) { if (i == array.indexOf(currValue) || i % 2) return currValue; return ''; }).join(''); span.innerHTML = text;
<p id="demo">Hello, Hello how are you! </p>

It splits by any sequence of non-word characters and then maps the array and if the item is repeated it returns empty string, else the item. I also save the separators so they can be recovered later - thus the check i % 2 ( if it is a separator always return it - we don't want to filter them).

Sign up to request clarification or add additional context in comments.

Comments

1

You can capture each word, and if the word has not been found yet, add it to a list of unique items. If the word is found, replace it.

function dedupe(string) { var unique = []; return string .replace(/[a-z]+/gi, function(m) { if (unique.indexOf(m) < 0) { unique.push(m); return m; } return ''; }) .replace(/\s+/, ' '); } let fixed = dedupe('Hello, Hello how are you!'); console.log(fixed);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.