replace string

C

CTG

How do you

var test = "<JIIM><BEAM><JIIM2><BEAM2>";
var x1= "><";
var x2= ">\\r\\n<";
test = test..replace(x1,x2);

why it complains when doing teh replace?
is teh syntax correct?
 
R

rf

CTG said:
How do you

var test = "<JIIM><BEAM><JIIM2><BEAM2>";
var x1= "><";
var x2= ">\\r\\n<";
test = test..replace(x1,x2);

Read the above very slowly.

test equals test dot dot replace...
 
C

CTG

var test = "<SD><RI><XYYYY>";
stringOut("Before conversion"+test);
var x1= "><";
var x2= ">\r\n<";
test = test.replaceAll(x1,x2);
stringOut("After conversion"+test);


Yes sure it was a .. but now whats wrong with this
 
R

RobG

        var test  = "<SD><RI><XYYYY>";
stringOut("Before conversion"+test);

Error: stringOut is not defined.

Replaced it with alert.
          var x1= "><";
          var x2= ">\r\n<";
        test = test.replaceAll(x1,x2);
stringOut("After conversion"+test);

Error: stringOut is not defined, replaced with alert.
Yes sure it was a  .. but now whats wrong with this

Once the errors are fixed as indicated, nothing as far as I can tell.
The result is exactly as expected:

After conversion<SD>
<RI><XYYYY>


What do you think is wrong with that?
 
R

RobG

Error: stringOut is not defined.

Replaced it with alert.

Oh, forgot to hightlight this one too:

Error: test.replaceAll is not a function

used:

test = test.replaceAll(x1,x2);
 
L

Lasse Reichstein Nielsen

CTG said:
var test = "<SD><RI><XYYYY>";
stringOut("Before conversion"+test);
var x1= "><";
var x2= ">\r\n<";
test = test.replaceAll(x1,x2);
stringOut("After conversion"+test);


Yes sure it was a .. but now whats wrong with this

When you want others to help with a problem, you need to provide three
things:
1. What you did. The code is sufficient, but make sure that it is the
*exact* code that provokes the problem, and that all helper functions
and other environment is included. In this case, you didn't say what
stringOut was. We can guess, but we can be wrong. It's better to
replace it by "alert" if you know it isn't important.
And make sure the code runs (there is no replaceAll method on strings)!

2. What you expect to happen. This is completely missing.

3. What actually happens. This is missing too. We can try running your
code, but we won't know if what we see is what you see (e.g., the bug
could be in your browser).

Without that, at best we can guess, and at worst we won't bother
trying.

That said, I *guess* that you want to replace all instances of
"><" with ">\r\n<" in a string.
To replace more than the first instance, you should use a
regexp with the global flag to do the matching:
test = test.replace(/></g, ">\r\n<");

/L
 
S

SAM

Le 10/16/09 4:23 AM, CTG a écrit :
var test = "<SD><RI><XYYYY>";
stringOut("Before conversion"+test);
var x1= "><";
var x2= ">\r\n<";
test = test.replaceAll(x1,x2);
stringOut("After conversion"+test);


Yes sure it was a .. but now whats wrong with this


Where did you find 'replaceAll' ?
^^^

To replace all items use the flag 'g' (global match):

var test = "<SD>\r\t<RI>\r\t<XYYYY>"
test = test.replace(/\r/g, '');
// or :
// test = test.replace(/\s+/g, '');
// or :
// test = test.replace(/>\s+</g, '><');
// all depends what you want

var test = "<SD>\r\t<RI>\r\t<XYYYY>"
var repl = new RegExp( '>[\r\t]+<', 'g');
// or :
// var repl = new RegExp( '>\s+<', 'g');
test = test.replace( repl, '><');


info:
<https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top