“JS String to Regex” Kode Jawaban

JS String to Regex

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

JS cocok dengan string nomor apapun

const match = 'some/path/123'.match(/\/(\d+)/)
const id = match[1] // '123'
foloinfo

JavaScript ekspresi reguler

// Tests website Regular Expression against document.location (current page url)
if (/^https\:\/\/example\.com\/$/.exec(document.location)){
	console.log("Look mam, I can regex!");
}
Kaotik

string ke regex javascript

function stringToRegex(s, m) {
  return (m = s.match(/^([\/~@;%#'])(.*?)\1([gimsuy]*)$/)) ? new RegExp(m[2], m[3].split('').filter((i, p, s) => s.indexOf(i) === p).join('')) : new RegExp(s);
}

console.log(stringToRegex('/(foo)?\/bar/i'));
console.log(stringToRegex('#(foo)?\/bar##gi')); //Custom delimiters
console.log(stringToRegex('#(foo)?\/bar##gig')); //Duplicate flags are filtered out
console.log(stringToRegex('/(foo)?\/bar')); // Treated as string
console.log(stringToRegex('gig')); // Treated as string
 Run code snippetHide results
Hurt Hamster

Jawaban yang mirip dengan “JS String to Regex”

Pertanyaan yang mirip dengan “JS String to Regex”

Lebih banyak jawaban terkait untuk “JS String to Regex” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya