Regex to replace everything except numbers and a decimal point

Regex to replace everything except numbers and a decimal point

Asked on January 11, 2019 in Regex.
Add Comment


  • 3 Answer(s)

    In regex to replace everything except numbers and a decimal point use this:

    document.getElementById(target).value = newVal.replace(/[^0-9.]/g, "");
    
    
    Answered on January 11, 2019.
    Add Comment

    Alternately try this code Regex to replace everything except numbers and a decimal point:

    document.getElementById(target).value = newVal.replace(/^\d+(\.\d{0,2})?$/, "");
    
    
    Answered on January 11, 2019.
    Add Comment
      • public string RemoveNonNumber(string Number,bool Money)
            {
              string NewString;
        
              NewString = Regex.Replace(Number, @"^.[^0-9]", "");
        
             
              return NewString;
            
            }

        but when try to tell regex ^- (Not equal dash) it does not work at all ,

        here is what i tried

         

         public string RemoveNonNumber(string Number,bool Money)
            {
              string NewString;
        
              NewString = Regex.Replace(Number, @"^-^.[^0-9]", "");
        
             
              return NewString;
            
            }

         

    Answered on May 20, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.