4

I've copied the first number from the windows calculator, and typed the second one. In Chrome console I get:

"‭65033‬" == "65033"
//false

65033‬ == 65033
//Uncaught SyntaxError: Invalid or unexpected token

It seems there is an unknown character at the beginning and end of it.

1) Is there a way to trim all "strange" characters without knowing them a priori?

2) Why does the windows calculator puts such chars in the number?

Edit: Was not explicit in the question, but any chars with valid information, such as ã,ü,ç,¢,£ would also be valid. What I don't want is characters that do not carry any information for the human reader.

mathiasfk
  • 1,278
  • 1
  • 19
  • 38
  • 1) Sure, you can strip out anything that isn't a digit. 2) Who knows. – Dave Newton Jan 26 '17 at 13:10
  • It seems that there's some invisible characters `\xe2\x80\xac` after the first 65033. – hsfzxjy Jan 26 '17 at 13:15
  • @DaveNewton Thanks, for the case of number input that would solve. I guess I didn't explicit that, but I would expect other characters as well (it's for a "search anything" function). – mathiasfk Jan 26 '17 at 13:21
  • "What I don't want is characters that do not carry any information for the human reader". How do you define this? – Oriol Jan 26 '17 at 13:57
  • I just copied a value from windows calculator and perform the same thing on chrome console without any problems (`"123" == "123"` returned `true`) – Ron Dadon Jan 26 '17 at 14:02
  • @Oriol visible characters maybe? – mathiasfk Jan 26 '17 at 14:10

2 Answers2

1

Edit: after the edit of the original question, this answer no longer offers a bulletproof solution.

var myNumber = 'foo123bar';
var realNumber = window.parseInt(myNumber.replace(/\D*/g, ''), 10);

What this does?

It replaces all the non-digit characters with empty character and then parses the integer out of numbers left in the string.

Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74
  • Note: `"foo123bar".replace(/\D*/, '') === "123bar"`. Probably not what's intended. Use `/\D/g` to replace all non-digits. – Alex K Jan 26 '17 at 13:51
0

A quick solution for this case:

eval("65033‬ == 65033".replace(/[^a-zA-Z0-9 =-_.]/, ''))

You can place your copied text in a string, then remove all unnecessary characters (by explicitly listing the ones that should stay there).

These may include non-alphanumerical characters + hyphen, underscore, equality, space et cetera - actual character that need to stay there will depend on your choice and needs.

Alternatively, you may try to remove all non-printable characters, as suggested here.

Finally, evaluate resulting code. Remember this is not necessarily the best idea for production code.

Community
  • 1
  • 1
rubikonx9
  • 1,403
  • 15
  • 27