0

I need to remove semicolon from a string in microsoft JSCRIPT.

    var Test="{abcdef};"

I need to remove the semi colon from the string Test. but it is throwing errror. I am using the following two ways to remove ";" from string

1)  var reg= new RegExp(";", "g")
    var Test=Test.replace(reg,"")



2)     var Test= Test.replace(/;/g,"");

Any Help appreciated

Thanks

  • 1) What does the error say? 2) Is it JScript for Windows Script Host (.js files run using wscript/cscript) or JScript.NET (compiled)? – Helen Jul 25 '14 at 09:17

1 Answers1

0

Your

var Test="{abcdef};"

creates a new string variable Test. To change its value and assign the result to the 'same' variable, write

Test=Test.replace(reg,"")

not

var Test=Test.replace(reg,"")

which will clobber Test before the .replace method is executed.

Evidence:

JScript 10.0.30319
% var t = "a;b;c;"
a;b;c;
% var t = t.replace(/;/g, "")
Error: Object required
% var t = "a;b;c;"
a;b;c;
% t = t.replace(/;/g, "")
abc

(This is JScript.NET 10.0.30319; same results for WSH JScript 5.7.16599)

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96