SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace with function in javascript

    Hi, people!

    I need to find all numbers and multiply them by ten in
    document.getElementById('numbers').innerHTML

    I know that it can be done by php with
    Code PHP:
    $text = preg_replace("|\d+|e", "\\0*10", $text)

    But how it can be done by javascript?

  2. #2
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, thanks, it is quite useful.
    But i just know how to replace with fixed string
    str.replace(/\d+/g, "12345")

    But is it possible in JavaScript to replace it with function? Something like
    str.replace(/\d+/g, \0*10) ?

  4. #4
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is very close!

    But what is wrong with

    Code JavaScript:
    str = "123";
    document.write (str.replace(/(\d+)/, "$1"*1))
    Here, $1 just cannot be converted to number. It prints NaN.
    Though if it is "$1"+1, it prints 1231 (as normal string).

    Is it possible to convert $1 to number?


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
  •