javascript:去除输入字符串上的换行符

发布于 2024-12-18 18:34:06 字数 1348 浏览 1 评论 0原文

我见过很多关于在文本区域输入中保留换行符的问题,但我需要相反的。我怀疑我的问题的答案不会是一个好的答案,但是......

我正在使用一张预先格式化的电子卡(在Convio中,如果这对任何人有帮助的话)。我可以添加我想要的任何文本和 HTML,但从表单传入的数据是通过 Convio 标签进行的,无法更改。

这就是我要说的。文本区域输入为:

Hello,

How are you?

这在电子贺卡中将如下所示:

Hello,<br />
<br />
How are you?

换句话说,它确实插入了换行标记,但它还在每个标记后放入了实际的换行符。没问题吧?这将正确显示,因为电子邮件阅读器忽略 HTML 中的实际换行符,但不幸的是,附加信息被添加到文本区域输入中,因此进入电子贺卡的实际上是:

A message from Mr. John Smith [email protected].<br />

Hello,<br />
<br />
How are you?

姓名和电子邮件地址来自其他字段形式。当然,这显示为:

A message from Mr. John Smith [email protected].    
Hello,

How are you?

问题是:我讨厌第一行。我的意思是我真的很讨厌它。该信息已经在其他地方的电子卡上,但我不希望它在那里。它太不友好了,它在电子贺卡上在视觉上没有吸引力,它几乎总是换行到第二行(看起来更糟糕,特别是在第二行和文本区域的第一行之间没有空格),它只是让我想哭。

如果所有内容都以字符串形式出现,没有实际的换行符,那么我可以毫无问题地对文本进行切片和切块以删除第一行。我可以编辑输入表单以删除文本区域输入中的实际中断,但是“来自...的消息”消息之后的那两个换行符让我很恼火。我不知道如何使输入字符串适合 JavaScript。

我仅限于 javascript - 是的,我知道这在 PHP 中很容易做到,但我没有这个选项 - 我和我的网站管理员几乎得出了这样的结论:我们不能做我做的事情想做。不过,我必须仔细检查,并且我希望有人能证明我错了。

I've seen lots of questions about preserving line breaks in textarea inputs, but I need the opposite. I suspect the answer to my question is not going to be a good one, but...

I'm working with a preformatted e-card (in Convio, if that helps anyone). I can add any text and HTML I want, but the data that's coming in from the form is via Convio tags and can't be changed.

Here's what I'm talking about. The textarea input is:

Hello,

How are you?

This would come into the e-card as:

Hello,<br />
<br />
How are you?

In other words, it does insert the break tags, but it also puts in an actual line break after each tag. No problem, right? That will display properly since the email reader ignores the actual line breaks in the HTML, but unfortunately additional information is prepended to the textarea input, so what comes into the e-card is really:

A message from Mr. John Smith [email protected].<br />

Hello,<br />
<br />
How are you?

With the name and email address coming from other fields in the form. This, of course, displays as:

A message from Mr. John Smith [email protected].    
Hello,

How are you?

Here's the issue: I hate that first line. I mean I really hate it. The info is already on the e-card elsewhere and I don't want it there. It's too unfriendly, it's visually unappealing on the e-card, it almost always wraps to a second line (which looks even worse, particularly without space between that and the first line from the textarea), and it just makes me want to cry.

If everything came in as a string without the actual line breaks, I'd have no problem slicing and dicing the text to remove the first line. I could edit the input form to remove the actual breaks in the textarea input, but those two line breaks after the "A message from..." message are killing me. I can't figure out how to make the input string palatable for javascript.

I am limited to javascript--yes, I know this would be simple to do in PHP, but I don't have that option--and my webmaster and I have pretty much come to the conclusion that we can't do what I want to do. I had to double-check, though, and I'd love for someone to prove me wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梨涡少年 2024-12-25 18:34:06

获取包含消息的元素的innerHTML,并从第一行中提取子串。

http://jsfiddle.net/Q8gu8/4/

grab the innerHTML of which ever element has the message, and substring out the first line.

http://jsfiddle.net/Q8gu8/4/

许你一世情深 2024-12-25 18:34:06

您可以使用 "\n" 匹配换行符(或使用 \r 表示回车符)。因此,如果我有以下字符串:

Foo
is
An
Apple

我可以像这样删除第一行:

// assume the string is stored in variable `str`
str = str.replace(/^.+?\n/, '');

... 这意味着我剩下:

is
An
Apple

/^.+?\n/ 是一个正则表达式,这意味着:匹配第一个换行符之前的所有内容(包括第一个换行符)。

You can match a line-break using "\n" (or \r for a carriage return). So, if I have the following string:

Foo
is
An
Apple

I can remove the first line like so:

// assume the string is stored in variable `str`
str = str.replace(/^.+?\n/, '');

... which means I'm left with:

is
An
Apple

/^.+?\n/ is a regular expression which means: match everything up to and including the first line break.

朮生 2024-12-25 18:34:06

您应该能够将正则表达式与 replace()

请参阅此fiddle

HTML:

<div class="replace">
    A message from Mr. John Smith [email protected].<br />

Hello,<br />
<br />
How are you?
<div>

JS:

var haystack = $('.replace').html();

var regex = /A message.*/;
$('.replace').html(haystack.replace(regex, ""));

输出:

Hello,

How are you?

请注意,这是使用 JQuery 作为选择器。

You should be able to use a regex with replace()

See this fiddle.

HTML:

<div class="replace">
    A message from Mr. John Smith [email protected].<br />

Hello,<br />
<br />
How are you?
<div>

JS:

var haystack = $('.replace').html();

var regex = /A message.*/;
$('.replace').html(haystack.replace(regex, ""));

Output:

Hello,

How are you?

Note that this is using JQuery for the selector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文