Links
- On Unicode
- JavaScript uses UCS-2 encoding internally
- octal escape sequence
- Note that there’s one exception here: by
itself,
\0
is not an octal escape sequence. It looks like one, and it’s even equal to\00
and\000
, both of which are octal escape sequences — but unless it’s followed by a decimal digit, it acts like a single character escape sequence. Or, in spec lingo:EscapeSequence :: 0 [lookahead ∉ DecimalDigit]
. It’s probably easiest to define octal escape syntax using the following regular expression:\\(?:[1-7][0-7]{0,2}|[0-7]{2,3})
. - Octal escapes have been deprecated in ES5
- They produce syntax errors in strict mode
- Note that there’s one exception here: by
itself,
- hexadecimal escapes
- Unicode escapes
- You could define Unicode escape syntax using the following regular expression:
\\u[a-fA-F0-9]{4}
.
- You could define Unicode escape syntax using the following regular expression:
- ECMAScript 6: Unicode code point escapes
- Characters without special meaning can be escaped as well (e.g.
'\a' == 'a'
), but this is of course not needed. However, using\u
outside of a Unicode escape sequence, or\x
outside of a hexadecimal escape is disallowed by the specification, and causes some engines to throw a syntax error. \v
and\0
escapes are not allowed in JSON strings.
- String
- Number and String
- tag: String
- Properties
- Static methods of String
- Methods
- StringView - based on typed arrays
- Character Access
charAt(i)
is much more widely supported than indexing /[i]
- localeCompare (compatibility)
- String primitives vs. instances
- String() → a safer toString()
- Handles
null
andundefined
safely.
- Handles
- article: JavaScript has a Unicode problem
Methods
Ref: String Methods
Method | Description |
---|---|
indexOf (searchValue[, fromIndex]) |
Returns -1 if the value is not found. |
split ([separator][, limit]) ] |
separator can be a string or a regex. If omitted, you get a array of one element containing the string. |
substr (start[, length]) |
start can be negative and is equivalent to string.length + start . If abs(start) > string.length or length is less than 1, you get an empty string. |
substring (indexA[, indexB]) |
indexA is inclusive and indexB is exclusive. If either is negative, they are treated as zero and if either is greater than the string length, they're set equal to the string length. |
Gotchas
Ref: String primitives vs. instances
var s_prim = "foo"; var s_obj = new String(s_prim); console.log(typeof s_prim); // "string" console.log(typeof s_obj); // "object" console.log("===:", s_prim === s_obj); // false console.log("=== + valueOf:", s_prim === s_obj.valueOf()); // true
Snippets
// gist function isString(obj) { return Object.prototype.toString.call(obj) == '[object String]'; }; // capture global on init var isString = (function() { var _toString = Object.prototype.toString; return function isString(obj) { return _toString.call(obj) == '[object String]'; }; })();