SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    FBI secret agent digitman's Avatar
    Join Date
    Sep 2004
    Location
    Work
    Posts
    697
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The replace() method (URGENT!!!!!)

    Hi guys,
    I need to know how I can replace all spaces in a string with a * character, and then replace all * back with spaces. For example, if a string is 'bla bla bla', i want it to be changed to 'bla*bla*bla' , and then bla*bla*bla back to bla bla bla.

    I tried the string.replace() method, but it only replaces the first space and leaves the other ones untouched, (i.e bla*bla bla, not bla*bla*bla). I read somewhere that I need to specify a regexp to have everything replaced but I don't know how to do regexps.

    Any ideas?

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var a = "bla bla bla";
    alert(a.replace(/\s/g, "*")):

    this will alert bla*bla*bla

    cheers

  3. #3
    FBI secret agent digitman's Avatar
    Join Date
    Sep 2004
    Location
    Work
    Posts
    697
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks!!

    I really gotta learn this regexp stuff for javascript. Where'd you learn it?

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its all about the flags...
    g = global - for replacing in all ponts of the string
    i = non case sensitive
    cheers


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
  •