Reputation: 55
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.
Upvotes: 1
Views: 624
Reputation: 10771
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.
Upvotes: 0
Reputation: 1308
grab the innerHTML of which ever element has the message, and substring out the first line.
Upvotes: 1
Reputation: 111870
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.
Upvotes: 1