JS-字符串

String Literals:enclose the characters of the string within a matched pair of single of double quotes.

ECMAScript3,string literals must be written on a single line.In ECMAScript 5,however you can break a string literal across multiple lines by ending each line with a backslash.Neither the backslash nor the line terminator that follow it are part of the string literal.

var aa = 'one\
two\
at the same line!'
//one two at the smame line!

Note that when you use single quote to delimit your strings, you must be carefull with English contractions and possessives.Since apostrophe is the same as the single-quote character,you must use the backslash character to escape any apostrophes.

 

When combining JS and HTML, it is a good idea to use one style of quotes for JS and the other style for HTML.

 

Escape Sequences:the backslash character has a special purpose in JS.

JS-字符串
 if the backslash character precedes any character other than those shown above,the backslash is simply ignored.For example, \# is the same as #.

 

Working with Strings:

Remember that strings are immutable in JS.Methods like replace() and toUpperCase() return new strings:they do not modify the string on which they are invoked.

In ECMAScript 5, strings can be treated like read-only arrays, and you can access individual characters from a string using square brackets instead of the charAt() method.

s = 'hello'
a = s[1]
console.log('a:' +a)

 

 Pattern Match:RegExps are not one of the fundamental types of JS,like Dates,they are simply a specialized kind of object,with a useful API.Text between a pair of slashes constitutes a regular expression literal.The second slash in the pair can also be followed by one or more letters,which modify the meaning of the pattern.




你可能感兴趣的:(字符串)