SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot Tiger_Tom's Avatar
    Join Date
    Feb 2000
    Location
    U.K.
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help: 1. transpose individual characters 2. Reverse a string

    Can Javascript do these sorts of
    regular expressions:

    1. Transpose individual characters in a string
    e.g.
    swop ewAr455MN for 12345ABCD, where 1 becomes e,
    2 becomes w, 3 becomes A and so on?

    2. Reverse a string?

    Is there tutorial that explains how to do this
    succinctly?

    I can do this in Perl, but the JavaScript
    tutorials I've found don't seem to offer these
    options.

    Any clues gratefully received.

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use regexp to do this

    http://tech.irt.org/articles/js049/

    heres a quick example

    PHP Code:
    <script type="text/javascript">

      
    myString = new String('1234567890');

      
    rExp = /6/gi;

      
    newString = new String ('m');
      
    results myString.replace(rExpnewString);

      
    document.write(results);

    </script> 
    this will reverse a string as well

    PHP Code:
    <script type="text/javascript">

      function 
    Rstr(str)
      {
        var 
    rev='';

        for (
    str.length-1a>=0i--)
        {
          
    rev+=str.charAt(a);
        }

        
    document.write(rev);

      }

    </script> 

  3. #3
    SitePoint Zealot Tiger_Tom's Avatar
    Join Date
    Feb 2000
    Location
    U.K.
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for taking the trouble to do that.

    What I wanted was to do replace one *set* of individual characters with another *in one go*,
    basically an associative array, I suppose.

    If I understand your code, it replaces only one
    character, is that right?

    What I'm aiming for is a kind of encryption or encoding of a string, not swopping individual characters. I should have made that clearer.

    The characters would be swopped back later, giving
    the original string.

    Your help has given me some ideas.
    Last edited by Tiger_Tom; Jun 10, 2002 at 13:52.

  4. #4
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually it replaces all occurances
    PHP Code:
    <script type="text/javascript">

      
    myString = new String('61626364656768669606');

      
    rExp = /[6]/gi;

      
    newString = new String ('m');
      
    results myString.replace(rExpnewString);

      
    document.write(results);

    </script> 
    becomes 'm1m2m3m4m5m7m8mm9m0m'

    rExp = /[6]/gi;

    as g = global, thus search and replacing the whole string.

  5. #5
    SitePoint Zealot Tiger_Tom's Avatar
    Join Date
    Feb 2000
    Location
    U.K.
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thank you.


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
  •