Show Menu
Cheatography

JavaScript Array, String and RegExp Cheat Sheet by

JavaScript Standard build-in objects

Create an Array

By literal way
[elem­ent0, eleme­nt1, ..., eleme­ntN]
new Array(­el­eme­nt0, eleme­nt1[, ...[, eleme­ntN]])
new Array(­ar­ray­Len­gth)
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­­­m­en­­­ts) or iterable object (object where you can get its elements, such as Map and Set).
Array.o­f(­el­eme­nt0[, eleme­nt1[, ...[, eleme­ntN­]]])
 
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­thi­n(­tar­get, start[, end = this.l­ength])
 
target
Target start index
 
start
Source start index, if it is negative, treated as length + start
fill(­val­ue[, start = 0[, end = this.l­eng­th]])
 
value
Value to fill an array
sort([­co­mpa­reF­unc­tion])
 
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­(s­tart, delet­eCo­unt[, 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.
 
dele­teC­ount
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_a­rray = old_a­rra­y.c­on­cat­(v­alu­e1[, value2[, ... value­N]])
slice
var shall­ow_­copy = arr.s­li­ce(­[b­egin[, end]])
 
begin
Zero-b­ased. If negative, indicate an offset from the end
join
str = arr.j­oi­n([­se­par­ator = ','])
indexOf
index = arr.i­nd­exO­f(­sea­rch­Ele­ment[, fromI­ndex = 0])
lastIn­dexOf
index = arr.i­nd­exO­f(­sea­rch­Ele­ment[, fromI­ndex = 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 undef­ined
findIndex
return the found Index or -1
 
callb­ack[, thisArg]
 
call­back
curr­ent­Value
  
index
  
array
reduce
accumulate
reduce­Right
accumulate from end
 
callb­ack[, initi­alV­alue]
 
call­back
prev­iou­sVa­lue
  
curr­ent­Value
  
curr­ent­Index
  
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 Arra­y.p­rot­oty­pe.­fu­n.c­al­l(­arr­ay-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­(t­ext)
`string text ${vari­able}`
template strings
Create by Char codes
String.fr­omC­har­Cod­e(­num1[, ...[, numN]])

String instance methods unrelated to HTML

concat
str.c­on­cat­(s­tri­ng2, strin­g3[, ..., strin­gN])
repeat
str.r­ep­eat­(c­ount)
count will convert to integer
'abc'.repeat(2) -> 'abcabc'
includes
endsWith
startsWith
str.­fu­ncN­ame­(­sea­rch­Str­ing[, posit­ion])
 
sear­chS­tring
A string to be searched for within this string
 
posi­tion
search from, optional
 
case-s­ens­iti­vity, return true or false

String instance methods related with RegExp

search
str.s­ea­rch­(r­egexp)
 
return the index of first match or -1
match
str.m­at­ch(­re­gexp)
same as regex­Obj­.e­xec­(str)
 
return an array containing the entire match result or null
 
result: input, index, 0, 1-n
replace
str.r­ep­lac­e(­reg­exp­|­sub­str, newSu­bSt­rfu­nct­ion)
 
regexp
substr
pattern
 
newSubStr
function
replac­ement
  
newS­ubStr 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
  
func­tio­n'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.a­nc­hor­(n­ame)
 
create and display an anchor (name property) in a document
e.g. <a name="n­ame­"­>st­r</­a>
link
str.l­in­k(­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
/patt­ern­/­flags
cons­tru­ctor
new RegExp­(p­att­ern[, flags])
 
patt­ern
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

RegE­xp.l­as­tIn­dex
The index at which to start the next match

RegEx Quick Reference

Char­acter 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
Anch­ors
^abc$
start / end of the string
\b \B
word, not-word boundary
Escaped charac­ters
\. * \\
escaped special characters
\t \n \r
tab, linefeed, carriage return
Groups & Lookar­ound
(abc)
capture group
\1
backre­ference to group #1
(?:abc)
non-ca­pturing group
(?=abc)
positive lookahead
(?!abc)
negative lookahead
Quan­tifiers & 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

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

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
          jasmine JS testing Cheat Sheet