String.fromCodePoint
:
let str = String.fromCodePoint(0x48, 0x69, 0x20, 0x1F310, 0x21) // 'Hi 🌐!'
str = String.fromCharCode(0x48, 0x69, 0x20, 0xD83C, 0xDF10, 0x21)
str.codePointAt(i)
method yields code point starting at UTF-16 offset i
str.codePointAt(3)
is 127760 or 0x1F310str.codePointAt(4)
is a “high surrogate” value between 0xDC00 and 0xDFFF[...str] // [ 'H', 'i', ' ', '🌐' ]
str.charAt(i)
/s[i]
yields a string of length 1 whose character has char code str.charCodeAt(i)
.
substring(fromInclusive, toExclusive)
slice
is like substring, but allows negative index values:
'Hello'.slice(-3, -1) // 'll'
indexOf(str)
, lastIndexOf(str)
yield index of first, last substring, or -1 if not found.startsWith(str)
, endsWith(str)
, includes(str)
yield Boolean result.trim
, trimStart
, trimEnd
yield substring obtained by dropping leading and/or traling white space (Unicode space/line/paragraph separators).str.split(separator)
yields an array of substrings around the separator.
'Mary had a little lamb'.split(' ') → ['Mary', 'had', 'a', 'little', 'lamb']
'Mary had a little lamb'.split(' ', 4) → ['Mary', 'had', 'a', 'little']
''
splits into UTF-16 code units. Use [...str]
instead.str.padStart(len)
, str.padEnd(len)
yields a string with space characters added to start/end whose length is ≥ len
str.repeat(n)
yields the string n
times.str.concat(x, y, ...)
yields str
followed by the string conversions of all arguments.str.toUpperCase()
, str.toLowerCase()
yields the string converted to uppercase/lowercasestr.replace(original, replacement)
replaces the first occurrence.
'moon'.replace('o', 'astod') → 'mastodon'
$
in the replacement string—see below.match
, matchAll
, search
, additional versions of replace
toLocaleUpperCase
, toLocaleLowerCase
, localeCompare
, normalize
encodeURI
, encodeURIComponent
, decodeURI
, decodeURIComponent
encodeURI('http://hello 🌐.com') → 'http://hello%20%F0%9F%8C%90.com'
[0-9]+ | Sequence of digits (at least one) |
[1-9]|1[0-2] | 1 ... 9 or 10, 11, 12 |
".*" | Quoted string |
https?://[a-zA-Z]+\.com | Dot com site |
/[0-9]+/
[ ^ $ . | ? * + ( )
as well as \
and, in literals, /
/https?:\/\/[a-zA-Z]+\.com/
RegExp('https?:\\/\\/[a-zA-Z]+\\.com')
new RegExp(...)
i
makes match case-insensitive./[a-z]+\.com/i
const regex = RegExp(/[a-z]+\.com/, 'i')
i |
ignoreCase |
Ignore case |
g |
global |
Find all matches |
m |
multiline |
^ , $ match start, end of line |
s |
dotAll |
. matches newline |
u |
unicode |
Match Unicode characters, not code units |
y |
sticky |
Match must start at regex.lastIndex |
source
, flags
, ignoreCase
, global
, ...
regex.source → '[a-z]+\\.com' regex.flags → 'i' regex.ignoreCase → true
regex.test(str)
is true
if str
contains a match::
/[0-9]+/.test('agent 007') → true
/^[0-9]+$/.test('agent 007') → false
regex.exec(str)
yields array with information about the match:
/[0-9]+/.exec('agent 007') → ['007', index: 6, input: 'agent 007']
g
(global) flag:
let digits = /[0-9]+/g digits.exec('555-1212') // Matches555
, setsdigits.lastIndex
to 3 digits.exec('555-1212') // Matches1212
, setsdigits.lastIndex
to 8 digits.exec('555-1212') // Returnsnull
, setsdigits.lastIndex
to 0
lastIndex
digits = /[0-9]+/y digits.lastIndex = 3 digits.exec('555-1212') → null digits.lastIndex = 5 digits.exec('555-1212') → ['212', index: 5, ...]
(...)
to group hours, minutes, am/pm:
let time = /([1-9]|1[0-2]):([0-5][0-9])([ap]m)/
exec
result as [1]
, [2]
, ...
time.exec('12:15pm') → ['12:15pm', '12', '15', 'pm', index: 0, ...]
time = /(?<hours>[0-9]|1[0-2]):(?<minutes>[0-5][0-9])([ap]m)/ time.exec('12:15pm') → ['12:15pm', '12', '15', 'pm', groups: { hours: '12', minutes: '15' }, ...]
(?:http|ftp)://(.*)
/(['"]).*\1/ matches'Fred'
or"Fred"
but not"Fred'
/(?<quote>['"]).*\k<quote>/
str.match(regex)
returns the match data:
'555-1212'.match(/[0-9]+/) → ['555',index: 0, ...]
'555-1212'.match(/[0-9]+/g) → [ '555', '1212' ]
str.matchAll(regex)
returns a lazy iterable of the match data.
str.search(regex)
returns the index of the first match or -1.str.replace(regex, replacementString)
replaces the first (or, with g
flag, every) match:
'555-1212'.replace(/[0-9]/g, '#') → '###-####'
$` , $' | the portion before or after the matched string |
$& | matched string |
$n | the nth group |
$$ | dollar sign |
'hello'.replace(/[aeiou]/g, '$&$&$&') → 'heeellooo'
'Harry Smith'.replace(/([A-Z])[a-z]+ ([A-Z][a-z]+)/, (match, first, last) => `${last}, ${first}.`) → 'Smith, H.'Replacement function arguments:
.
matches a code unit, not a Unicode character:
/Hello .$/.test('Hello 🌐') → false
u
flag whenever the regex or matched string can have characters > U+FFFF:
/Hello .$/u.test('Hello 🌐') → true
\u{...}
to embed Unicode code points into pattern:
/Hello \u{1F310}/u
\p{...}
matches Unicode binary property:
'Hello 世界'.match(/\p{L}+/gu) → ['Hello', '世界']
L , Lu , Ll |
Letters, upper/lowercase letters |
N |
Numbers |
S |
Symbols |
P |
Punctuation |
'Hello 世界'.match(/\p{Script=Han}+/gu) → ['世界']
\P{...}
is complement of \p{...}
+
and *
repetitions are greedy:
'"Hi" and "Bye"'.match(/".*"/g) → ['"Hi" and "Bye"']
'"Hi" and "Bye"'.match(/".*?"/g) → ['"Hi"', '"Bye"']
(?= )
'7:32am'.match(/[0-9]+(?=[ap]m)/g) → ['32']
?<=
:
'7:32am'.match(/(?<=:)[0-9]+/g) → ['32']
?!
, ?<!
'7:32am'.match(/(?<!:)[0-9]+/g) → ['7', '2']