Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

JavaScript_Regular_Expression_Methods

The document provides an overview of JavaScript regular expression methods, detailing their descriptions, return types, and examples of usage. It covers methods such as RegExp.test, RegExp.exec, string.search, string.match, string.matchAll, string.split, and string.replace. Additionally, it explains the arguments available in the replace function for handling matches.

Uploaded by

drpiga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

JavaScript_Regular_Expression_Methods

The document provides an overview of JavaScript regular expression methods, detailing their descriptions, return types, and examples of usage. It covers methods such as RegExp.test, RegExp.exec, string.search, string.match, string.matchAll, string.split, and string.replace. Additionally, it explains the arguments available in the replace function for handling matches.

Uploaded by

drpiga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

JavaScript Regular Expression Methods

method description returns example

const regex = /(\w+)\.(\w+)/g


const testString='files: kittens.jpeg, puppies.png'

RegExp.test determine whether or boolean regex.test(testString)


not a string matches a // true
regular expression
RegExp.exec extract regular match object regex.exec(testString)
expression match and // [
'kittens.jpeg', // full match
group match 'kittens', // groups (this obj has 2)
information for first 'jpeg',
match since the index: 7, // index at start of match
stored lastIndex input: 'files:\nkittens.jpeg\npuppies.png',
(see MDN reference) groups: undefined // object of named groups
]

string.search find index of first number testString.search(regex)


match // 7

string.match find matches against single match: testString.match(regex)


regular expression match object // [ 'kittens.jpeg', 'puppies.png' ]
multiple matches:
array of strings
string.matchAll return details about iterator of const matches = [ ...testString.matchAll(regex) ];
all matches match objects matches.map(match => [ match[1], match[2] ])
// [ [ 'kittens', 'jpeg' ], [ 'puppies', 'png' ] ]

v1.0 2020-02-25 https://bonnie.dev


JavaScript Regular Expression Split / Replace
method description returns example

string.split split string on array of const testString = 'a b c\t de';


regular expression strings const regex = /\s+/;
testString.split(regex);
// [ 'a', 'b', 'c', 'de' ]

string.replace replaces matches string const testString = 'She sells seashells by the seashore';
with fixed string or const regex = /\b(s)\w+\b/gi;
tongueTwister.replace(regex, 's-word');
output of function // 's-word s-word s-word by the s-word’
applied to match
(see below for testString.replace(regex, (match, first_letter) =>
arguments to function) `${first_letter}-word`);
// 'S-word s-word s-word by the s-word'

Arguments to replace function (runs once per match)


argument description example

const regex = /(\w+)\.(\w+)$/


const testString='1. kittens.jpeg'

match full match 'kittens.jpeg'

p1, p2, etc groups p1: 'kittens' p2: 'jpeg'

offset index at which match occurs 3

string full input string '1. kittens.jpeg'

groups object of named groups for const regex = /(?<root>\w+)\.(?<ext>\w+)$/


{ root: 'kittens', ext: 'jpg' }
.

v1.0 2020-02-25 https://bonnie.dev

You might also like