If I try to convert 003050 to a string it turns it into 1576 how can I turn 003050 into a string without it doing that. And any other possible whole number? I tried '' + 003050 and it's still 1576
String(003050);
"1576"
This has no relation with the conversion to string.
var n = 003050;
is enough to make the number interpreted as octal.
You would have gotten the same result with ""+parseInt("3050", 8).
Simply remove the leading 0 to get the number 3050 :
var n = 3050;
If you want a literal string with leading 0, well, just make it
var s = "003050";
Seems you could do with reading a little about Javascript,
Reference: Standard ECMA-262 5.1 Edition / June 2011
and understand primitives, objects and literals etc.
It is worth noting Additional Syntax
Past editions of ECMAScript have included additional syntax and semantics for specifying octal literals and octal escape sequences. These have been removed from this edition of ECMAScript. This non-normative annex presents uniform syntax and semantics for octal literals and octal escape sequences for compatibility with some older ECMAScript programs.
What you have, 003050, is a Numeric Literal
The syntax and semantics of 7.8.3 can be extended as follows except that this
extension is not allowed for strict mode code:
Syntax
NumericLiteral ::
DecimalLiteral
HexIntegerLiteral
OctalIntegerLiteral
OctalIntegerLiteral ::
0 OctalDigit
OctalIntegerLiteral OctalDigit
OctalDigit :: one of
0 1 2 3 4 5 6 7
And so, if not using strict mode, where this syntax has been removed ,then 003050 (an octal) is the decimal value 1576.
If you had, '003050', then you would have a String Literal
A string literal is zero or more characters enclosed in single or double quotes. Each character may be represented by an escape sequence. All characters may appear literally in a string literal except for the closing quote character, backslash, carriage return, line separator, paragraph separator, and line feed. Any character may appear in the form of an escape sequence.
So, you really wanted to use a string literal, didn't you?
If you really meant to use an octal numeric literal and wanted it as a string, then you would need to do something like this.
Javascript
var x = 003050,
y = ('000000' + x.toString(8)).slice(-6);
console.log(y);
Output
003050
On jsFiddle
Related
I'm writing some code that scans a string for TeX-style Greek character (like \Delta or \alpha), and replaces the string with the Unicode symbol. It works fine for the non-italic Greek characters. The problem is that I want to use mathematical italic for the lower case. These codes are one digit longer. For example, the code for the letter alpha is 1d6fc. When I put \u1d6fc into my string it displays as the character that matches \u1d6f (a lower case m with a superimposed tilde) followed by the letter c. How do I force the "correct" reading of the code?
You have to use UTF-16 surrogate pairs for characters beyond the UTF-16 range. In your particular case, you can use 0xD835 0xDEFC:
console.log('\uD835\uDEFC')
Here is a handy pair calculator. If you don't have to worry about Internet Explorer, you can also use String.fromCodePoint(), which will deal with that mess for you. If you do have to worry about Internet Explorer, MDN has a polyfill for that method.
To produce a \u escape sequence with more than 4 hex digits (code point belonging to a so-called astral plane), you can use the Unicode code point escape notation \u{xxxxx}:
console.log ('\u{1d6fc}');
or you can call String.fromCodePoint with the code point value expressed in hexadecimal using the 0x prefix notation:
console.log (String.fromCodePoint (0x1d6fc));
What is the logic behind the output of the following examples:
console.log('\272') // -> º
console.log('\364') // -> ô
As far as I know, \ is an escape character in javascript which means it tries to escape the following character but in the first example it is not equal to ASCII code of 72 which is character H.
That's because of the octal encoding.
Any character with a character code lower than 256 (i.e. any character
in the extended ASCII range) can be escaped using its octal-encoded
character code, prefixed with . (Note that this is the same range of
characters that can be escaped through hexadecimal escapes.)
To use the same example, the copyright symbol ('©') has character code
169, which gives 251 in octal notation, so you could write it as
'\251'.
You can take a look to this explanation, quite illustrative: https://mathiasbynens.be/notes/javascript-escapes
They are octal values.
You can find all of them here
However, using octal values are deprecated. Using them in strict mode will throw you SyntaxError.
I'll suggest you to use Hexadecimal code instead which you can find in the provided link:
For octal value 272, the hexadecimal value is BA. So, you'll use it prefixed by small letter x - denoted as hex value.
console.log('\xBA') // -> º
i have the following regex that allows only alphabets :
/[a-zA-Z]+/
a = "abcDF"
if (a.match(/[a-zA-Z]+/) == a){
//Match
}else{
//No Match
}
How can I do this using p{L} (universal - any language like german, english etc.. )
What I tried :
a.match(/[p{l}]+/)
a.match(/[\p{l}]+/)
a.match(/p{l}/)
a.match(/\p{l}/)
but all returned null for the letter a = "aB"
Starting with ECMAScript 2018, JavaScript finally supports Unicode property escapes natively.
For older versions, you either need to define all the relevant Unicode ranges yourself. Or you can use Steven Levithan's XRegExp package with Unicode add-ons and utilize its Unicode property shortcuts:
var regex = new XRegExp("^\\p{L}*$")
var a = "abcäöüéèê"
if (regex.test(a)) {
// Match
} else {
// No Match
}
If you are willing to use Babel to build your javascript then there's a babel-plugin I have released which will transform regular expressions like /^\p{L}+$/ or /\p{^White_Space}/ into a regular expression that browsers will understand.
This is the project page: https://github.com/danielberndt/babel-plugin-utf-8-regex
You may use \p{L} with the modern ECMAScript 2018+ compliant JavaScript environments, but you need to remember that the Unicode property classes are only supported when you pass u modifier/flag:
a.match(/\p{L}+/gu)
a.match(/\p{Alphabetic}+/gu)
will match all occurrences of 1 or more Unicode letters in the a string.
NOTE that \p{Alphabetic} (\p{Alpha}) includes all letters matched by \p{L}, plus letter numbers matched by \p{Nl} (e.g. Ⅻ – a character for the roman number 12), plus some other symbols matched with \p{Other_Alphabetic} (\p{OAlpha}).
There are some things to bear in mind though when using u modifier with a regex:
You can use Unicode code point escape sequences such as \u{1F42A} for specifying characters via code points. Normal Unicode escapes such as \u03B1 only have a range of four hexadecimal digits (which equals the basic multilingual plane) (source)
"Characters of 4 bytes are handled correctly: as a single character, not two 2-byte characters" (source)
Escaping requirements to patterns compiled with u flag are more strict: you can't escape any special characters, you can only escape those that can actually behave as special characters. See HTML input pattern not working.
i have the following regex that allows only alphabets :
/[a-zA-Z]+/
a = "abcDF"
if (a.match(/[a-zA-Z]+/) == a){
//Match
}else{
//No Match
}
How can I do this using p{L} (universal - any language like german, english etc.. )
What I tried :
a.match(/[p{l}]+/)
a.match(/[\p{l}]+/)
a.match(/p{l}/)
a.match(/\p{l}/)
but all returned null for the letter a = "aB"
Starting with ECMAScript 2018, JavaScript finally supports Unicode property escapes natively.
For older versions, you either need to define all the relevant Unicode ranges yourself. Or you can use Steven Levithan's XRegExp package with Unicode add-ons and utilize its Unicode property shortcuts:
var regex = new XRegExp("^\\p{L}*$")
var a = "abcäöüéèê"
if (regex.test(a)) {
// Match
} else {
// No Match
}
If you are willing to use Babel to build your javascript then there's a babel-plugin I have released which will transform regular expressions like /^\p{L}+$/ or /\p{^White_Space}/ into a regular expression that browsers will understand.
This is the project page: https://github.com/danielberndt/babel-plugin-utf-8-regex
You may use \p{L} with the modern ECMAScript 2018+ compliant JavaScript environments, but you need to remember that the Unicode property classes are only supported when you pass u modifier/flag:
a.match(/\p{L}+/gu)
a.match(/\p{Alphabetic}+/gu)
will match all occurrences of 1 or more Unicode letters in the a string.
NOTE that \p{Alphabetic} (\p{Alpha}) includes all letters matched by \p{L}, plus letter numbers matched by \p{Nl} (e.g. Ⅻ – a character for the roman number 12), plus some other symbols matched with \p{Other_Alphabetic} (\p{OAlpha}).
There are some things to bear in mind though when using u modifier with a regex:
You can use Unicode code point escape sequences such as \u{1F42A} for specifying characters via code points. Normal Unicode escapes such as \u03B1 only have a range of four hexadecimal digits (which equals the basic multilingual plane) (source)
"Characters of 4 bytes are handled correctly: as a single character, not two 2-byte characters" (source)
Escaping requirements to patterns compiled with u flag are more strict: you can't escape any special characters, you can only escape those that can actually behave as special characters. See HTML input pattern not working.
Just tested in Chrome:
"\054321"
entered in the console gives:
",321"
Clearly "\054" gets converted to ",", but I fail to find a rule for this in the specification.
I am looking for a formal specification for this rule. Why does "\054" produce ","?
Update: It was an OctalEscapeSequence, as defined in B 1.2.
If you look at an ascii table you will find that the octal value 054 (decimal: 44) is a comma.
You can find more on the Values, variables, and literals - JavaScript | MDN page that specifies:
Table 2.1 JavaScript special characters
Character Meaning
XXX The character with the Latin-1 encoding specified by up to three octal digits XXX between 0 and 377. For example, \251 is the octal sequence for the copyright symbol.
So essentially, if you use an escape code of 3 digits, it will be evalutated as an octal value.
That said, be aware that octal escapes are deprecated in ES5 (These have been removed from this edition of ECMAScript) and you should use heximal or unicode escape codes instead (\XX or \uXXXX).
If you wish to have the literal string \012345 then simply use a double backslash: \\012345.
The Oct code for , is 054.
Check this Ascii Table
Why you have to put "\" before that Oct code then??
From a quick search you'll find lots of solutions. But this one gave me a better clearification.
Octal escape sequences
Any character with a character code lower than 256 (i.e. any character in the extended ASCII range) can be escaped using its octal-encoded character code, prefixed with . (Note that this is the same range of characters that can be escaped through hexadecimal escapes.)
To use the same example, the copyright symbol ('©') has character code 169, which gives 251 in octal notation, so you could write it as '\251'.
[The upper section was collected from an article which is here]
There are also some exceptions which you can find from that article.
Because using \ in a string, as in other languages like C or Python, implies a special character, like \r as carriage return or \n as newline. In this case, It's the ascii value of the coma, which is 054.
"\054" is octal for ",". See here: http://www.asciitable.com/index/asciifull.gif