SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Jun 10, 2002, 10:36 #1
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.
-
Jun 10, 2002, 12:40 #2
- 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(rExp, newString);
document.write(results);
</script>
PHP Code:<script type="text/javascript">
function Rstr(str)
{
var rev='';
for (a = str.length-1; a>=0; i--)
{
rev+=str.charAt(a);
}
document.write(rev);
}
</script>
-
Jun 10, 2002, 13:44 #3
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.
-
Jun 10, 2002, 14:58 #4
- 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(rExp, newString);
document.write(results);
</script>
rExp = /[6]/gi;
as g = global, thus search and replacing the whole string.
-
Jun 10, 2002, 15:29 #5
Bookmarks