“string ke regex javascript” 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

Menggunakan Regex di JavaScript

//Adding '/' around regex
var regex = /\s/g;
//or using RegExp
var regex = new RegExp("\s", "g");
TC5550

cara menggunakan fungsi kecocokan di javascript untuk 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.
Helpless Hamster

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 “string ke regex javascript”

Pertanyaan yang mirip dengan “string ke regex javascript”

Lebih banyak jawaban terkait untuk “string ke regex javascript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya