removing spaces from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pavanpvss
    New Member
    • Apr 2008
    • 9

    removing spaces from a string

    hi,
    can anybody tell me a javascript regular expression which removes spaces in a string? I am catching a value in a javascript function which contains space.For example ' pavan kumar' or 'pavan kumar '
    I need only to remove before or after space. The space between pavan and kumar should be as it is. Could anybody help me?

    Thanks,
    pavan
  • vee10
    New Member
    • Oct 2006
    • 141

    #2
    hello

    this solve ur problem

    [CODE=javascript]function checkspaces()
    {
    debugger
    var strValue = document.getEle mentById("txt1" ).value;
    var objRegExp = /^(\s*)(\b[\w\W]*)$/;

    if(objRegExp.te st(strValue)) {
    //remove leading a whitespace characters
    strValue = strValue.replac e(objRegExp, '$2');
    }
    document.getEle mentById("txt1" ).value = strValue;

    }[/CODE]



    Originally posted by pavanpvss
    hi,
    can anybody tell me a javascript regular expression which removes spaces in a string? I am catching a value in a javascript function which contains space.For example ' pavan kumar' or 'pavan kumar '
    I need only to remove before or after space. The space between pavan and kumar should be as it is. Could anybody help me?

    Thanks,
    pavan
    Last edited by gits; Apr 29 '08, 07:36 AM. Reason: added code tags

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Create a new trim() method:
      [code=javascript]String.prototyp e.trim = function() {
      return this.replace(/^\s+|\s+$/g,"");
      }[/code]

      Comment

      Working...