I am trying to create some random unicode strings within javascript and was wondering if there was an easy way. I tried doing something like this...
var username = "David Perry" + "/u4589";
But it just appends /u4589 to the end which is to be expected since it's just a string. What I WANT it to do is convert that into the unicode character in the string (AS IF I typed ALT 4589 on the keypad). I'm trying to build the string within javascript because I wanna test my form with various symbols and stuff and I'm tired of trying ALT codes to see what weird characters there are... so I thought.. I would loop through ALL unicode characters for FUN and populate my form and submit it automatically...
I was going to start at /u0000 and go up to /uffff and see which codes break my website when outputting them :)
I know there are different functions in JS but I can't seem to figure out why I can't build a string of unicode characters. lol.
If it's too complicated don't worry about it. It's just something I wanted to tinker with.
If you wish to generate random characters or loop through a range of characters, then you could use
String.fromCharCode()
, which gives you the character with the Unicode number passed as argument, e.g.String.fromCharCode(0x4589)
orString.fromCharCode(i)
wherei
is a variable with an integer value.Both the
\uxxxx
notation and theString.fromCharCode()
work up to 0xFFFF only, i.e. for Basic Multilingual Plane characters. This may well suffice, but if you need non-BMP characters, check out e.g. the MDN page on fromCharCode.Try
"\u4589"
instead of"/u4589"
:the forward slash (
/
) is just a forward slash in a string, however the backslash (\
) is an escape character.