开发者

Do Web Pages Create Newlines on their Own?

开发者 https://www.devze.com 2023-04-10 03:31 出处:网络
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>?

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 cod开发者_Go百科e will work?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号