“string para regex javascript” Respostas de código

String js para regex

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

JS corresponde a qualquer string de número

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

Usando regex em javascript

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

Como usar a função de correspondência em javascript para 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

Expressão regular JavaScript

// 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 para 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

Respostas semelhantes a “string para regex javascript”

Perguntas semelhantes a “string para regex javascript”

Mais respostas relacionadas para “string para regex javascript” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código