SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Test returns true, but replace does nothing.

    This is driving me freaking crazy. I have a RegExp, and a String. When I alert RegExp.test(String), I get "true." Sweet. Then I run String.replace(RegExp, ""), in an attempt to get rid of whatever was matched. But that does not change the string at all.

    Am I misunderstanding replace, or what?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Possibly. Here's some info on using it.
    https://developer.mozilla.org/en/Cor...String/replace

    You may also want to post some of the code you're having trouble with.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't change a string, no matter what you do to it.

    All you can do is assign a new string to the variable that refers to the old string.

  4. #4
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, poor wording on my part. Here's what I meant:

    Code:
    var a = "This is some kind of string.",
    b = /^.+?s/;
    if (b.test(a)) {
        alert("Yeah, it's there.");
        a = a.replace(b, "");
    }
    When I did a much more complicated version of this, I'd get the alert, but "a" wouldn't be any different. It turned out the RegExp I was using was wrong (that's why "replace" wasn't doing anything), but I still don't know how it could test, but not replace.

    I'd post the code, but after changing both the string and the RegExp, I was finally able to get it to work (at about 1:00 this morning), so I don't have those exact examples anymore.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by sdleihssirhc View Post
    I'd post the code, but after changing both the string and the RegExp, I was finally able to get it to work (at about 1:00 this morning), so I don't have those exact examples anymore.
    What you have there is a fine example of how simplifying the problem and explaining it to someone else, can help you to solve it for yourself.

    Well done.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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