Results 1 to 5 of 5

Thread: 'replace()' function and parameters

  1. #1
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default 'replace()' function and parameters

    I've been experimenting with the 'replace()' function in JavaScript. I was doing kind of a BBCode thing for fun to see what 'replace()' could do. I know MOSTLY how to use the 'replace()' function, but some questions have arose after reading this thread and the 2nd post by jscheuer1:

    http://www.dynamicdrive.com/forums/s...ad.php?t=38863

    Well, in this code:

    Code:
    replace(/(\d *)x( *\d)/g, '$1×$2');
    how does the first parameter work? I know that it fetches a character, but what's up with the \d and * stuff? It would be very helpful if someone could explain this using as much knowledge as they have about it.

    Thanks much in advance.

    -magicyte

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    As you know, replace() method could accept regular expressions.

    The first parameter is a regular expression which matches strings in either of this format:
    • 123456789x123456789
    • 123456789 x 123456789


    \d Finds any single digit:

    * Finds zero or more occurences of a regular expression

    /g is a global search of all occurences of a pattern.

    () Finds the group of characters inside the parenthesis.

    For further reading:
    http://www.javascriptkit.com/jsref/regexp.shtml
    http://en.wikipedia.org/wiki/Regular_expression
    http://www.codeproject.com/KB/dotnet/regextutorial.aspx
    http://www.webreference.com/js/column5/
    Last edited by rangana; 11-19-2008 at 01:44 AM. Reason: Removed typo
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    replace(/(\d *)x( *\d)/g, '$1×$2');
    Let's break it down:

    \d * - matches any occurrence of a single digit followed by 0 or more spaces.

    Making it (\d *) means that the Regular Expression Object will store a reference to this match, as it is the first stored reference, it will later be accessible as $1.

    The x matches a literal lowercase x character. Since there are no parentheses, it will not be stored.

    ( *\d) - is similar to the first part, only now matches 0 or more spaces followed by a digit and stores that as $2.

    So, in something like:

    255 x 150

    it will match:

    5 x 1

    and replace it with:

    5 × 1
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Thanks, guys! I appreciate the help.

    -magicyte

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
  •