Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace string with new line

Status
Not open for further replies.

jaschulz

Programmer
May 20, 2005
89
FR
I want to test various regexp against a specific piece of text, so I wrote a function that creates a new regexp from user input. It works fine, except that if I try to replace a given string with a new line (i.e. "\n"), I get the backslash and the "n" instead of a new line. My function looks like this:

function doIt() {
var wTA = document.getElementById('workingTextArea');
var workStr = wTA.value;
var iA = document.getElementById('inputArea');
var fA = document.getElementById('flagArea');
var rA = document.getElementById('replaceArea');
var formatStr = iA.value;
var flagStr = fA.value;
replaceStr = rA.value;
if (formatStr.length > 0) {
var rE = new RegExp(formatStr,[flagStr]);
workStr=workStr.replace(rE,replaceStr);
wTA.value = workStr;
} else {alert("No format string entered."); }
}

The problem occurs when replaceStr = "\n". I though I could deal with this by setting replaceStr to "\\n" but that just gets me two backslashes and then the "n".

What am I doing wrong here?

Thanks,

JAs
 
What am I doing wrong here?
According to your problem's description - nothing.

Here's a working example:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

window.onload = function () {
   var t = document.getElementById("t");
   var n = "\n";
   t.value = t.value.replace(/b/g, n);
}

</script>

<style type="text/css"></style>

</head>
<body>

<textarea id="t">aaabaaa</textarea>

</body>
</html>

When this page loads it changes the value in the textarea from aaabaaa to aaa\naaa

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
The following works just fine:
Code:
str1="abcxdef"
str2=str1.replace("x","\n")
alert(str2)
From this, maybe one can conclude that getting the value from the "replaceArea" is not preserving it's essential "string-ness"? What happens if you throw in a little debug:
Code:
...
replaceStr = rA.value;
[red]alert("before" +replaceStr+ "after")[/red]
if (formatStr.length > 0) {
...

_________________
Bob Rashkin
 
\n isn't an HTML new line, <br> is.

Lee, for the value of a textarea (and inputs of type text) newlines are represented by \n instead of <br>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
I didn't see anything in the original post that indicated the target was a text area. Should have figured that, though, since the OP was referring to value and not innerHTML.

Lee
 
even with "innerHTML", he should have gotten a space, not the \n. That is, document.getElementById('divName').innerHTML="abc\n123", where "divName" is a <div>, will give abc 123 and not abc\n123

_________________
Bob Rashkin
 
I didn't see anything in the original post that indicated the target was a text area.

Actually, I was just guessing based on the id of the element he was modifying - var wTA = document.getElementById('[!]workingTextArea[/!]');

even with "innerHTML", he should have gotten a space, not the \n. That is, document.getElementById('divName').innerHTML="abc\n123", where "divName" is a <div>, will give abc 123 and not abc\n123

The reason for this is because when assigning the javascript string it will assign the literal value to the innerHTML of the div.

If you had HTML defined like so:
Code:
<div>
abc
123
</div>
It would look like this on the page:
Code:
abc 123
Fortunately the rules stay the same when assigning the innerHTML values from a javascript string.


-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Any reason why you're passing a 1-element array as the second parameter in the RegExp constructor? I'm pretty sure it's supposed to receive a string for the second parameter - but I usually use the / / syntax for my regexp instead of the constructor so I'm not sure:
Code:
var rE = new RegExp(formatStr,[!][[/!]flagStr[!]][/!]);

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
No reason (I had a reason once, I'm sure, but I can't remember it now). It works if you use just the plain flagStr, or, rather, it gives the same problem either way. I don't think this is a regexp problem. It seems to me the problem is in replace().

JAS
 
I visited your site and I see the problem. When pulling the value from the input it's grabbing the literal value \n instead of the newline character. Hade you made the input for the replaceArea a <textarea> instead and hit enter, then it would have passed the newline character (since that's what would exist in the <textarea>). Anyway, to fix this problem for newlines, you'll just have to run a replace on the replaceArea value:

Code:
replaceStr = rA.value[!].replace(/\\n/g, "\n")[/!];

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
[purple]You're welcome[/purple]

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top