“match js regexp” Réponses codées

Regex match mot js

let string = "Hello world"; // you can use .toLowerCase() to check only in lower

let match = "world"; // word to match

let regex = new RegExp('\\b('+match+')\\b');

console.log('true for no match, false for match');
console.log(string.match(regex) == null); //.match() returns array of words found

// true for no match, false for match
// false
L'homme habile

JS String à Regex

const regex = new RegExp('https:\\/\\/\\w*\\.\\w*.*', 'g');
garzj

match exact javascript regex

var r = /^a$/

function matchExact(r, str) {
   var match = str.match(r);
   return match && str === match[0];
}
Krushn

JavaScript Regex Exemple 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"]
POG

match js regexp

// 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] }
*/
Puzzled Puffin

match js regexp

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

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

Réponses similaires à “match js regexp”

Questions similaires à “match js regexp”

Plus de réponses similaires à “match js regexp” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code