Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Regex match word js

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

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

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

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
Comment

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

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 :: js absolute value 
Javascript :: jquery if element is clicked 
Javascript :: validatorjs get all errors 
Javascript :: javascript check if set 
Javascript :: nth value of the Fibonacci sequence in js 
Javascript :: javascript print out 
Javascript :: javascript average function 
Javascript :: hide and show modal jquery 
Javascript :: how to create a button with react 
Javascript :: random in a range js 
Javascript :: settimeout 
Javascript :: express get ip address of request 
Javascript :: get middle of string js 
Javascript :: nock CORS error 
Javascript :: ajax authorization header token 
Javascript :: remove everything except alphabet and number js 
Javascript :: access to xmlhttprequest at from origin http localhost:3000 has been blocked by cors policy 
Javascript :: javascript confirm delete 
Javascript :: javascript for loop starting from end 
Javascript :: nextjs path alias 
Javascript :: javascript stop delay 
Javascript :: get current time in javascript 
Javascript :: s3 list objects in folder node js 
Javascript :: how to add a shadow react native 
Javascript :: how to return 5 records instead of 10 records in datatable in laravel 
Javascript :: javascript use camera 
Javascript :: export variables react 
Javascript :: how to find length of array js 
Javascript :: use regex to get urls from string 
Javascript :: committing only some changes to git 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =