4

Hi I have a string like below:

 >123456<

How can I easily replace the angle brackets and replace them with blank?

I have tried the below:

                        mystring.replace(/>/g, "");
                        mystring.replace(/</g, "");

However if I do an alert(mystring); on this it is still showing with the angle brackets?

Ctrl_Alt_Defeat
  • 3,801
  • 12
  • 59
  • 110

6 Answers6

11

You need to assign, in this case, mystring with the result of the operation.

var s = '>123456789<';
s = s.replace(/[<>]/g, '');
alert(s);
Leonard
  • 2,922
  • 2
  • 28
  • 50
3

You are not setting it back to the string:

mystring = mystring.replace(/>/g, "");
mystring = mystring.replace(/</g, "");

As in Zanathel's answer, use a single regex for this [<>] thats cleaner than 2 statements.

mystring = mystring.replace(/[<>]/g, "");
techfoobar
  • 63,712
  • 13
  • 108
  • 129
2
mystring = mystring.replace(/>|</g, '')
dfsq
  • 187,712
  • 24
  • 229
  • 250
0
var xx =">123456<";
alert(xx.replace(">","").replace("<",""));
Dilip Godhani
  • 1,837
  • 3
  • 17
  • 32
0

In Javascript strings are immutable. Therefore, every time you make change to string a new string object is created.

This will work fine:

mystring = mystring.replace(/>/g, "");
mystring = mystring.replace(/</g, "");
Stardust
  • 1,094
  • 6
  • 21
  • 33
0

Try this.. It worked for me:

var str = ">123456<";
var ne = str.replace(">", "");
ne = ne.replace("<", "");

alert(ne);
Kremena Lalova
  • 521
  • 1
  • 4
  • 17