?
Solved

Replacing carriage returns

Posted on 2003-03-11
9
Medium Priority
?
775 Views
Last Modified: 2012-06-27
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
Comment
Question by:jamesgreen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 8110748
try this:

 function replaceChar(strOriginal,strChar,strReplace)
 {
    return strOriginal.split(strChar).join(strReplace);
 }
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 8110750
will also replace substrings as well as individual chars
0
 

Author Comment

by:jamesgreen
ID: 8111011
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 33

Expert Comment

by:knightEknight
ID: 8111695
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
 

Author Comment

by:jamesgreen
ID: 8112319
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
 

Author Comment

by:jamesgreen
ID: 8112336
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 8112812
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
 
LVL 33

Accepted Solution

by:
knightEknight earned 200 total points
ID: 8112824
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
 

Author Comment

by:jamesgreen
ID: 8150355
Thanks for your help with this.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question