[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 776
  • Last Modified:

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
0
jamesgreen
Asked:
jamesgreen
  • 5
  • 4
1 Solution
 
knightEknightCommented:
try this:

 function replaceChar(strOriginal,strChar,strReplace)
 {
    return strOriginal.split(strChar).join(strReplace);
 }
0
 
knightEknightCommented:
will also replace substrings as well as individual chars
0
 
jamesgreenAuthor Commented:
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?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
knightEknightCommented:
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?

0
 
jamesgreenAuthor Commented:
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)
0
 
jamesgreenAuthor Commented:
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)
0
 
knightEknightCommented:
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.
0
 
knightEknightCommented:
anyway ... here is how you do the replace:

str = str.replaceChar("\r","");
str = str.replaceChar("\n","<BR />");

and back again:

str = str.replaceChar("<BR />","\n");
0
 
jamesgreenAuthor Commented:
Thanks for your help with this.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now