Link to home
Start Free TrialLog in
Avatar of jamesgreen
jamesgreen

asked on

Replacing carriage returns

Hi hopefully an easy one,

I have a javascript function to replace characters in a text string with a different value:

     function replaceChar(strOriginal,strChar,strReplace){
          var regReplace = eval("/" + strChar + "/g");
          return strOriginal.replace(regReplace,strReplace);
     }

However, I can't get this function to remove line feeds or carriage returns.  If I pass in "\n" as the string to remove I get the following error:

        Error: "/" expected

Hope you can help.

James
Avatar of knightEknight
knightEknight
Flag of United States of America image

try this:

 function replaceChar(strOriginal,strChar,strReplace)
 {
    return strOriginal.split(strChar).join(strReplace);
 }
will also replace substrings as well as individual chars
Avatar of jamesgreen
jamesgreen

ASKER

Thanks, that works ok.

Is it ok to call this as:

replaceChar2(a.value, "\r\n", "z");

You see, I want to make sure I always replace one new line in the text with one line break (bearing in the mind the text actually contains a carriage return and line feed).

Also, I'm loading the value into XML using the standard IXML XMLparser - do I need to do any formating on the string to preserve the line breaks?
in the call above, you need to do this:

a.value = replaceChar2(a.value, "\r\n", "z");

As to your other questions, I don't understand exactly what you need.  Do you need to replace the CR-LF with another (set of) character(s)?  Or do you need to preserve them?  or add something to them?

This is want I want to do:
1. User enters text into HTML text box (may contain line breaks)
2. Load the text into an XML dom.
3. Apply an XSL stylesheet to the XML from 2 to produce an HTML table with the text entered in 1.  At this point, I will replace all line breaks I find in the XML with <BR/> tags
3. When the user saves the data I will extract the data from the table and replace the <br/> tags with line breaks.
4. Insert data from 3 into an XML dom.
5. Send the XML to a SQL server database where the new values are stored (including line breaks)
This is want I want to do:
1. User enters text into HTML text box (may contain line breaks)
2. Load the text into an XML dom.
3. Apply an XSL stylesheet to the XML from 2 to produce an HTML table with the text entered in 1.  At this point, I will replace all line breaks I find in the XML with <BR/> tags
3. When the user saves the data I will extract the data from the table and replace the <br/> tags with line breaks.
4. Insert data from 3 into an XML dom.
5. Send the XML to a SQL server database where the new values are stored (including line breaks)
You have 2 step 3's ... I'll call them 3a and 3b.

When you create the table in 3a, don't replace the linebreaks ... instead, make a copy of the text in a local variable, and replace the line breaks in the local.  Then use the local to create the table.

Then step 3b becomes unnecessary (assuming that the user hasn't changed the text in 3a -- which I do since it is now in a table insead of a textarea).

That way you don't ever have to change the text in your XML dom -- you can submit it as is to the server when the time comes.
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help with this.