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,933
  • 12
  • 66
  • 116

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
  • 3,012
  • 2
  • 31
  • 52
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
  • 65,616
  • 14
  • 114
  • 135
2
mystring = mystring.replace(/>|</g, '')
dfsq
  • 191,768
  • 25
  • 236
  • 258
0
var xx =">123456<";
alert(xx.replace(">","").replace("<",""));
Dilip Godhani
  • 2,065
  • 3
  • 18
  • 33
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,115
  • 6
  • 22
  • 33
0

Try this.. It worked for me:

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

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