0

I want to replace all the text in a webpage with saving the text length like:

From:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id dui tincidunt 

To:

sample text sample text sample text sample text sample text sample text sampl 

I'am new in Javascript and I don't understand it. è_é

Please help me.

1 Answer 1

1

Create a function that takes the string of words to replace, and the string to repeat until the length, then simply keep appending to a variable until the length is reached:

function replaceTextWithLength(textToReplace, replaceText) { var length = textToReplace.length; var text = ""; while (text.length < length) { text += replaceText; } return text.substring(0, length); } var words = replaceTextWithLength("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id dui tincidunt", "sample text "); console.log(words); 

Demo: http://jsfiddle.net/zdvz59yb/

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

1 Comment

How can i get all the body text ? element.body.innerText ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.