tags:

views:

51

answers:

2

I want to break the following string at the word To and then truncate the email address that follows at 15 characters using JavaScript. This is the sentence:

Please email this card to [email protected]

It should like like this:

Please email this card 
to email@emailadd...
+1  A: 

does your truncation need to be at the word "To" or just at a certain position?

you might want to look at different javascript functions like

  • substring to get a part of a string
  • charAt to return the character at position x.
  • indexOf to find the position at which a certain piece occurs.

for the truncation of the email-address, you might use a regular expression to find and possibly replace parts of it by certain characters.

if this doesn't help you, please tell us more about the pattern you would like to use for truncation of your text.

regards

Atmocreations
A: 

code:

var input = "Please email this card to [email protected]";
var toPos = input.indexOf(' to ');
var output = input.substr(0, toPos) + '\r\n' + input.substr(toPos + 1).substr(0, 17) + '...';

output:

Please email this card
to email@emailadd...
Sky Sanders