Forum Discussion

abrar22's avatar
abrar22
Frequent Contributor
9 years ago
Solved

Trim Special Characters from Numbers

Hi, 

 

I want to remove special characters from number. Can I use replace function ?

 

var Notional= Getter.vGetPropertyVal(WTbx_SingleLegNotional,'Text');
var Desired= Notional.replace(/[!@#$%^&*]/g, "");

 

Its coming as 1,000,000. But I want to trim all , from the number. Using above function I am gettign run time error. ANy suggestions?

 

  • You can try using this:

    var Notional= Getter.vGetPropertyVal(WTbx_SingleLegNotional,'Text');
    var Desired= Notional.replace(/\D/g, "");

     

     

    This code works as shown below

     

    function ReplaceSpChr(){
        var s = "/[!@2#$4%^&1*]0/g00,0";
        // Create regular expression pattern.
        var re = /\D/g;
        // Use replace
        var r = s.replace(re, "");
        return(r);
    
        // Output:  2410000
    }

     

4 Replies

  • djadhav's avatar
    djadhav
    Regular Contributor

    You can try using this:

    var Notional= Getter.vGetPropertyVal(WTbx_SingleLegNotional,'Text');
    var Desired= Notional.replace(/\D/g, "");

     

     

    This code works as shown below

     

    function ReplaceSpChr(){
        var s = "/[!@2#$4%^&1*]0/g00,0";
        // Create regular expression pattern.
        var re = /\D/g;
        // Use replace
        var r = s.replace(re, "");
        return(r);
    
        // Output:  2410000
    }

     

    • abrar22's avatar
      abrar22
      Frequent Contributor

      djadhav Thanks.

       

      So should I call this menthod and Pass Notional as parameter?

       

      ReplaceSpChr(Notional)

      ?

       

      Thanks

       

      • djadhav's avatar
        djadhav
        Regular Contributor

        Option 1:

        Try changing you code as follows first:

         

        var Notional= Getter.vGetPropertyVal(WTbx_SingleLegNotional,'Text');
        var Desired= Notional.replace(/\D/g, "");

         

        Option 2:

        If that has issues, which it should not, you could call the function with 'Notional' as parameter.

         

        function ReplaceSpChr(StringVal){
            // Create regular expression pattern.
            var re = /\D/g;
            // Use replace
            var r = StringVal.replace(re, "");
            return(r);
        }