SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace last digit(s) in string using regexp

    I think the subject heading pretty much sums it up...!

    I have a string with a whole bunch of characters in it. I need to replace the last group of numbers which appear in it with something else. I know there's an easy solution, but I can't seem to find it.

    Any help would be greatly appreciated! :-)
    Last edited by AtomicPenguin; Jan 17, 2003 at 19:58.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    specifics?

    I'm not really sure what you are asking...
    however you can use :
    Code:
    [stringName].length
    to get the length of the string.

    then use an for loop to loop through the string and read it into an array.

    Code:
    for (var i; i < [stringName].length; i++) {
      [arrayName][i] = [stringName].charAt(i);
    }
    then now you have your string in an array you can change whatever character you want...

    lastly all you need to do is loop through the array and read your new string into a variable in a similar way to the loop above.

    im not sure if my code is 100% correct but the ideas are all there (I hope) to do it right. I'm sure ill be corrected if I'm wrong!!
    Last edited by Lunchy_Munchy; Jan 17, 2003 at 23:04.

  3. #3
    SitePoint Evangelist compwizard's Avatar
    Join Date
    May 2002
    Location
    United States
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am assuming that you know how many chars you need to replace. If so here is some code:
    Code:
    	var origtext = "hello world!";
    	var replacetext=".";
    	var numCharsToRemove = 1;
    	var newtext = origtext.substring(0, (origtext.length-numCharsToRemove)) + replacetext;
    	alert (newtext);
    Compwizard
    "There are 10 kinds of people in this world -- those who know binary, and those who don't."


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
  •