www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Replace characters in a string

  1. #1
    Join Date
    Oct 2009
    Posts
    6

    resolved [RESOLVED] Replace characters in a string

    Hello, this should be an easy question.

    I have a string in which i want to replace a specific sequence of three final characters ("123") with a static character ("5"). For example, the string "hello123" needs to be "hello5", or "goodbye123" needs to be "goodbye5".

    I know charAt can index one character, then a subString method can replace that character with something else, but how can three characters be repaced with one?

    Many thanks.

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var str = "goodbye123".replace(/\d+$/, "5");
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Oct 2009
    Posts
    6
    Thanks for the reponse Fang.

    Wouldn't that replace any 3 digit combination with a "5"? If there is a string "goodbye987" I don't want to change it.

    I only want the three end characters replaced if they are "123".

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var str = "goodbye123".replace(/123$/, "5");
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    Oct 2009
    Posts
    6
    That worked great. Thanks!

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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