James
James

Reputation: 2253

Do Web Pages Create Newlines on their Own?

When text on a webpage wraps around to the next line because it hit the end of your template, is that creating a hidden newline in the code that is different from <br>?

I'm trying to do a .replace() in Javascript, but it doesn't find phrases that span two lines. Is there any way to get around this so my Javascript code will work?

Upvotes: 0

Views: 68

Answers (3)

jches
jches

Reputation: 4527

Include whitespace in part of your expression:

s.replace(/stuff on line1[\s]+stuff on line 2/, 'replacement');

Or just the newline characters \n and \r:

s.replace(/stuff on line1[\\n\\r]+stuff on line 2/, 'replacement');

There is a multi-line flag (/m), but it is reportedly not universal.

Upvotes: 0

deceze
deceze

Reputation: 522015

A newline (\n or <br />) is not the same thing as an automatic text wrap by the text layout engine. A \n or <br /> forces the layout engine to wrap the line and is detectable as a character (\n) or tag (<br />), but an automatic wrap does not produce a new character or tag. You can only test the height of the container to guess at whether you're dealing with automatically wrapped text or not.

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245399

No. Word wrapping inside the browser (or even inside an individual element) does not cause newlines to be inserted into the markup.

There's also really no good way to detect word wrapping in JavaScript.

You might want to post another question with your goal so that we might make suggestions on how to accomplish what you really want.

Upvotes: 3

Related Questions