Show Menu
Cheatography

JavaScript Array, String and RegExp Cheat Sheet by

JavaScript Standard build-in objects

Create an Array

By literal way
[element0, element1, ..., elementN]
new Array(element0, element1[, ...[, elementN]])
new Array(arrayL­ength)
By using the result of a math
an array is returned by
- RegExp.exec
- String.match
- String.re­place
By using Methods
Array.f­­r­o­m­(a­­rra­­yL­ike[, mapFn[, thisArg]])
 
an array-like object (object with a
length
property and indexed elements, such as
argue­­­me­n­­ts
) or iterable object (object where you can get its elements, such as
Map
and
Set
).
Array.of(element0[, element1[, ...[, elementN]]])
 
Every argument is considered as an element in the array.
Array.from
and
Array.of
work like
Array
constr­uctor to create an new array.

Array instance mutator methods

copyWi­thin(target, start[, end = this.l­ength])
 
target
Target start index
 
start
Source start index, if it is negative, treated as
length + start
fill(value[, start = 0[, end = this.l­eng­th]])
 
value
Value to fill an array
sort([compar­eFu­nction])
 
function compare(a, b) {
   if (a less than b, or a should be in front of b) {return -1;}
    if (a is greater than b or be behind of b) {return 1;}
 ­  return 0; // a must be equal to b
}
splice(start, delete­Count[, item1[, item2[, ...]]])
 
start
Index at which to start changing the array. If greater than the length, will set to length; if negative, will begin from the end.
 
delete­Count
If 0, will not delete any element. If Omitted, will be equal to
length - start
 
item1, item2
The elements to add to the array
 
returns an array containing the deleted elements
These methods modify the array

Array instance Accessor methods

concat
var new_array = old_array.concat(value1[, value2[, ... valueN]])
slice
var shallo­w_copy = arr.slice([begin[, end]])
 
begin
Zero-b­ased. If negative, indicate an offset from the end
join
str = arr.join([separator = ','])
indexOf
index = arr.indexOf(search­Element[, fromIndex = 0])
lastIn­dexOf
index = arr.indexOf(search­Element[, fromIndex = arr.length - 1])

Array instance Iteration methods

forEach
map
return new array
filter
return new array
every
return
true
if every element satisfies testing function
some
return
true
if at least one element satisfies testing function
find
return the found value or
undefined
findIndex
return the found Index or
-1
 
callback[, thisArg]
 
callback
curren­tValue
   
index
   
array
reduce
accumulate
reduce­Right
accumulate from end
 
callback[, initia­lValue]
 
callback
previo­usValue
   
curren­tValue
   
curren­tIndex
   
array
entries
key/value pairs
keys
keys
values
values
 
return an new
Array Iterator
object
Some of them can also use for array-like objects by Array.p­ro­totype.fun.call(array-like object, args)

All the Array instance methods

copyWithin
fill
pop
push
reverse
shift
sort
splice
unshift
concat
join
slice
toString
toLoca­leS­tring
indexOf
lastIn­dexOf
forEach
entries
every
some
filter
find
findIndex
keys
map
reduce
reduce­Right
values
 

Create a string

String literals
'string text'
"­string text"
String(text)
`string text ${vari­able}`
template strings
Create by Char codes
String.fr­omC­har­Code(num1[, ...[, numN]])

String instance methods unrelated to HTML

concat
str.concat(string2, string3[, ..., stringN])
repeat
str.repeat(count)
count will convert to integer
'abc'.repeat(2) -> 'abcabc'
includes
endsWith
startsWith
str.funcName(search­String[, position])
 
search­String
A string to be searched for within this string
 
position
search from, optional
 
case-s­ens­iti­vity, return
true
or
false

String instance methods related with RegExp

search
str.search(regexp)
 
return the index of first match or -1
match
str.match(regexp)
same as regexObj.exec(str)
 
return an array containing the entire match result or null
 
result: input, index, 0, 1-n
replace
str.replace(regexp|substr, newSubStr|function)
 
regexp
substr
pattern
 
newSubStr
function
replac­ement
   
newSubStr can include some special replac­ement patterns
 
$$
inserts a '$'
 
$&
inserts the matched substring
 
$`
inserts the portion of the string that precedes the matched string
 
$'
inserts the portion of the string that follows the matched substring
 
$n or $nn
n and nn are decimal digits, inserts the nth parent­hesized submatch string
   
function's result will be used as the replac­ement, and the arguments is:
 
match
like $&
 
p1, p2, ...
like $n
 
offset
The offset of the matched substring within the whole string
 
string
the whole string

String instance methods related HTML

anchor
str.anchor(name)
 
create and display an anchor (name property) in a document
e.g.
<a name="n­ame­"­>st­r</­a>
link
str.link(url)
 
create an HTML snippet for a hypertext link
e.g.
<a href="u­rl">­str­</a>

All the String instance methods

charAt
charCodeAt
concat
includes
endsWith
indexOf
lastIn­dexOf
locale­Compare
match
repeat
replace
search
slice
split
startsWith
substr
substring
toLoca­leL­owe­rCase
toLoca­leU­ppe­rCase
toLowe­rCase
toString
toUppe­rCase
trim
valueOf
anchor
link

Escape notation

\0
the NULL character
\'
single quote
\"
double quote
\\
backslash
\n
new line
\r
carriage return
\v
vertical tab
\t
tab
\b
backspace
\f
form feed
\uXXXX
unicode codepoint
\xXX
the Latin-1 character
 

Create a RegExp

literal notation
/pattern/flags
constr­uctor
new RegExp(pattern[, flags])
 
pattern
The text
 
flags
 
g
global match
 
i
ignore case
 
m
multiline
use
test
to test for a match in its string parameter.

Static property you may use

RegExp.la­stIndex
The index at which to start the next match

RegEx Quick Reference

Character classes
.
any character except newline
\w \d \s
word, digit, whitespace
\W \D \S
not word, digit, whitespace
[abc]
any of a, b, or c
[^abc]
not a, b, or c
[a-g]
character between a & g
Anchors
^abc$
start / end of the string
\b \B
word, not-word boundary
Escaped characters
\. * \\
escaped special characters
\t \n \r
tab, linefeed, carriage return
Groups & Lookaround
(abc)
capture group
\1
backre­ference to group #1
(?:abc)
non-ca­pturing group
(?=abc)
positive lookahead
(?!abc)
negative lookahead
Quanti­fiers & Altern­ation
a* a+ a?
0 or more, 1 or more, 0 or 1
a{5} a{2,}
exactly five, two or more
a{1, 3}
between one & three
a+? a{2,}?
match as few as possible
ab|cd
match ab or cd
Reference from regexr.com
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          AngularJS Cheat Sheet
          JavaScript Cheat Sheet
          Web Programming Cheat Sheet