Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript regex example match

//Declare Reg using slash
let reg = /abc/
//Declare using class, useful for buil a RegExp from a variable
reg = new RegExp('abc')

//Option you must know: i -> Not case sensitive, g -> match all the string
let str = 'Abc abc abc'
str.match(/abc/) //Array(1) ["abc"] match only the first and return
str.match(/abc/g) //Array(2) ["abc","abc"] match all
str.match(/abc/i) //Array(1) ["Abc"] not case sensitive
str.match(/abc/ig) //Array(3) ["Abc","abc","abc"]
//the equivalent with new RegExp is
str.match('abc', 'ig') //Array(3) ["Abc","abc","abc"]
Comment

how to use the match function in javascript for regex

const str = 'For more information, see Chapter 3.4.5.1';
const re = /see (chapter d+(.d)*)/i;
const found = str.match(re);

console.log(found);

// logs [ 'see Chapter 3.4.5.1',
//        'Chapter 3.4.5.1',
//        '.1',
//        index: 22,
//        input: 'For more information, see Chapter 3.4.5.1' ]

// 'see Chapter 3.4.5.1' is the whole match.
// 'Chapter 3.4.5.1' was captured by '(chapter d+(.d)*)'.
// '.1' was the last value captured by '(.d)'.
// The 'index' property (22) is the zero-based index of the whole match.
// The 'input' property is the original string that was parsed.
Comment

js regexp match

// 1. Match indices for numbered group
const matchObj = /(a+)(b+)/d.exec('aaaabb');

console.log(matchObj);
/*
Output -
['aaaabb', 'aaaa', 'bb', index: 0, input: 'aaaabb', groups: undefined, indices: Array(3)]
*/
// then
matchObj.indices[1];
/*
Output - 
[0, 4]
*/
// -------------------------

// 2. Match indices for named groups
const matchObj = /(?<as>a+)(?<bs>b+)/d.exec('aaaabb');

console.log(matchObj);
/*
Output - 
['aaaabb', 'aaaa', 'bb', index: 0, input: 'aaaabb', groups: {as: 'aaaa', bs: 'bb'}, indices: Array(3)]
*/
// then
matchObj.indices.groups;
/*
Output -
{ as: [0,4], bs: [4,6] }
*/
Comment

js regexp match

// ES2022
const matchObj = /(a+)(b+)/d.exec('aaaabb');

console.log(matchObj);
/*
Output -
['aaaabb', 'aaaa', 'bb', index: 0, input: 'aaaabb', groups: undefined, indices: Array(3)]
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript call php function with parameters 
Javascript :: what is a promise 
Javascript :: add parameter submit form javascript 
Javascript :: detect if overflow javascript 
Javascript :: add quotes to array items 
Javascript :: js remove first character from string 
Javascript :: Prevent Double tap in React native 
Javascript :: cors axios 
Javascript :: javascript array loop 
Javascript :: console.log 
Javascript :: How to loop through an object in JavaScript with the Object.values() method 
Javascript :: get smallest value in array js 
Javascript :: making axios call with headers 
Javascript :: string theory 
Javascript :: js reverse string 
Javascript :: javascript string error 
Javascript :: javascript console 
Javascript :: how to delete object properties in javascript 
Javascript :: js start at this character 
Javascript :: javascript error try catch 
Javascript :: react-slick 
Javascript :: is javascript faster than python 
Javascript :: jest mock mockname 
Javascript :: swr data fetching 
Javascript :: includes in js 
Javascript :: multi dimensional array javascript 
Javascript :: import npm dotenv package 
Javascript :: toast success 
Javascript :: byte number to array js 
Javascript :: javascript tostring 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =