www.webdeveloper.com
Results 1 to 8 of 8

Thread: remove underscore from string

  1. #1
    Join Date
    Apr 2010
    Posts
    22

    remove underscore from string

    I need to remove a underscore(_) from a string, I am using the replace function bt it does not get removed? Does anybody know why.


    here is the code

    Code:
    ISSUE_support = "Joe_Smith";
    ISSUE_support.replace("_"," ");

  2. #2
    Join Date
    Feb 2010
    Posts
    184
    Code:
    ISSUE_support = "Joe_Smith";
    ISSUE_support = ISSUE_support.replace("_"," ");
    Last edited by letmehaveago; 07-13-2010 at 08:31 AM. Reason: forgot something

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    1. replace() is a method. You need to give your variable the new value with an assignment:
    Code:
    ISSUE_support = "Joe_Smith";
    ISSUE_support=ISSUE_support.replace("_"," ");
    2. On the other hand are you aware that the method, as you have written, will replace only the first occurrence of the underscore in the string? If you need multiple replacements, you must use a Regular Expression:
    Code:
    ISSUE_support=ISSUE_support.replace(/_/g," ");
    Last edited by Kor; 07-13-2010 at 08:35 AM.

  4. #4
    Join Date
    Feb 2010
    Posts
    184
    Thanks for the second point.

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by letmehaveago View Post
    Thanks for the second point.
    The moment I have learned them, Regular Expressions changed my life

    http://lawrence.ecorp.net/inet/samples/regexp-intro.php

  6. #6
    Join Date
    Feb 2010
    Posts
    184
    I have been playing around with regular expressions for sometime too. I use it with vim.

    What I am concerned is http://www.w3schools.com/jsref/jsref_replace.asp doesn't even document that. I probably should default to https://developer.mozilla.org/en/Cor...String/replace but no browser compatibility list.

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by letmehaveago View Post
    I have been playing around with regular expressions for sometime too. I use it with vim.

    What I am concerned is http://www.w3schools.com/jsref/jsref_replace.asp doesn't even document that.
    Oh, but it does, See Example 3 : "Perform a global, case-insensitive search [...] "

  8. #8
    Join Date
    Feb 2010
    Posts
    184
    Oh, but it does, See Example 3 : "Perform a global, case-insensitive search [...] "
    Yes, but would have been more useful in the description or definition. It is a bit unintuitive for me too - php and java both replace all occurrences.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles