how to include a variable value into regex pattern? [duplicate] - javascript

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Regular Expression Pattern With A Variable
(4 answers)
Closed 8 years ago.
I want to include a value of variable into regex pattern.
The following code is the familiar.
var regex = /(.)\1{2}/;
I want to do like The following.
var number = 2;
var regex = /(.)\1{number}/
How to do that ?

var number = 2;
var regex = new RegExp('(.)\\1{'+number+'}');

Related

How can I pass the variable to the to RegEx? [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 2 years ago.
I want to pass the cookie as a variable .
letstr = cookie;
(/\bcookie\b/g).test('having cookie.');
Here you can pass the variable to the regEx
let x = "cookie";
let reg = new RegExp(\\b${x}\\b,'g');
x.test("having cookie");
\b will create /\b this. and let 'g' will be added to the end of the string

Why is this regex incorrect? [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I have the following regular expression:
\d{4}(-\d{2}){2}
Which is supposed to match dates that follow the YYYY-MM-DD format, so 1990-01-01 should successfuly match. However this fails when I try it in javascript.
var x = new RegExp('\d{4}(-\d{2}){2}')
x.test('1990-02-01') //why is this false?
Use the regular js regex syntax. Like this:
var x = /\d{4}(-\d{2}){2}/;
console.log(x.test('1990-02-01'));
if you want to keep the new RegExp part, you have to escape the string's backslashes:
var x = new RegExp('\\d{4}(-\\d{2}){2}');
console.log(x.test('1990-02-01'));

JavaScript RegExp concatinating variable [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
Trying to replace all a's in the string ignoring the case.
str = "All a's will be replaced";
str.replace(/a/gi, 'Aa');
As expected, the above line of code produces Aall Aa's will be replAaced
But,
regExp = new RegExp('/a/gi');
str.replace(regExp, 'Aa');
the above line with the use of RegExp pattern produces original string All a's will be replaced
How to achieve expected result?

can't get second number between parenthesis using regex [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
now I begin learning regex, and I have set of string in format like "(9/13)", and I need get second number. I try this regex: /\(.*?[^\d]*(\d+?)\)/g, in online regex it works normally.
But here:
var d = "(9/13)";
var v = /\(.*?[^\d]*(\d+?)\)/g;
alert(d.match(v));
it returns "(9/13)" , what am I doing wrong?
const source = "(9/13)";
const re = /\/(\d+)\)/;
console.log('result', re.exec(source).pop())
you can use Regex.exec() to find the number
const source = "(9/13)";
const re = /\(\d+\/(\d+)\)/;
console.log(re.exec(source)[1])

Javascript regex in attribute [duplicate]

This question already has answers here:
Create RegExps on the fly using string variables
(6 answers)
Closed 8 years ago.
I need to use data attribute for regex just as
<div data-regex="REGEX-HERE">
and then get the value by javascript and put in a variable. and then do a test like
var regex = $(this).attr("data-regex");
regex.test(name)
when I tried to use "^[\x20-\x7E]+$" for testing english character is didn't work.
Note when I tried this
var regex = /^[\x20-\x7E]+$/;
It worked.
Thanks in advance
You can do this:
var regex = new RegExp("^[\x20-\x7E]+$",""); // Modifiers on the tend
So finally:
var regex = new RegExp($(this).data("regex"));
regex.test(name)

Categories

Resources