How to make my own string in javascript as if I hi

2019-07-19 20:48发布

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.

2条回答
在下西门庆
2楼-- · 2019-07-19 20:58

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) or String.fromCharCode(i) where i is a variable with an integer value.

Both the \uxxxx notation and the String.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.

查看更多
Viruses.
3楼-- · 2019-07-19 21:02

Try "\u4589" instead of "/u4589":

>>> "/u4589"
"/u4589"
>>> "\u4589"
"䖉"

the forward slash (/) is just a forward slash in a string, however the backslash (\) is an escape character.

查看更多
登录 后发表回答