Why is this regex incorrect? [duplicate] - javascript

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'));

Related

String starting with backslash hacks my regex [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 3 years ago.
My string should be in the IRC command format : "/add john".
So, i created this Regex :
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test('\/add user1');
alert(bool);
The problem is either I use /***/ or RegExp syntax, if I set a backslash at the beginning of my string (like in my example above), my alert pop up show "true" and I don't want that.
I code in Javascript
You can use String.raw to make sure that the backlash is not removed when testing your input:
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test(String.raw`\/add user1`);
alert(bool);
You can play with this code here: https://jsbin.com/ziqecux/25/edit?js

regex match number in parentheses (Firefox) [duplicate]

This question already has answers here:
Javascript regex (negative) lookbehind not working in firefox
(4 answers)
Closed 4 years ago.
I have string like this:
(3) Request Inbox
Now I want to detect number 3 in parentheses. Pay attention just 3. I write this regex in javascript but it doesn't work in Firefox.
var regex = new RegExp(/(?<=\()\d+(?:\.\d+)?(?=\))/g);
Error in console: SyntaxError: invalid regexp group
Demo link
Positive lookbehind is not supported by most browsers so use an alternative way to get your answer.
Something like this,
var string = "somestring(12)";
var exp = /(?:\()(\d+)(?:\.\d+)?(?=\))/;
var mat = string.match(exp);
if(mat) {
console.log(mat[1]);// second index
}
This should only give 12 as the answer
For a full match try this:
var regex = new RegExp(/(?=\d+\))\d+/g);

using RegExp constructor result into wrong output [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I have a regex expression which i use for email address validation. It is working fine as literal expression but showing different result when using the RegExp constructor.
var emailpattern=/^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/g;
console.log(emailpattern.test('nithin#gmail.com'))//true
var obj = new RegExp('^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$','g');
console.log(obj.test('nithin#gmail.com'))//false
You need to remove quotes for the pattern which you have specified in the new RegExp() to get rid of extra overhead of escaping the \ character:
var emailpattern=/^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/g;
console.log(emailpattern.test('nithin#gmail.com'))//true
var obj = new RegExp(/^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,'g');
console.log(obj.test('nithin#gmail.com'))//false
Escaping the \ character:
var emailpattern=/^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/g;
console.log(emailpattern.test('nithin#gmail.com'))//true
var obj = new RegExp('(([^<>()\\[\\]\\\.,;:\s#"]+(\\.[^<>()\\[\\]\\\.,;:\\s#"]+)*)|(".+"))#((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))','g');
console.log(obj.test('nithin#gmail.com'))//false

How to use dynamic variable between regular expression [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 6 years ago.
I have string which contains some date and some comma separated values like this
var a = "1,13,20160308,200500000012016,10,Pending,01-02-2016,1|#|1,13,20160418,200500000012016,10,Pending,08-03-2016,1|#|1,13,20160623,200500000012016,10,Pending,18-04-2016,1|#|1,13,20160803,200500000012016,10,Pending,23-06-2016,1|#|1,13,20160912,200500000012016,10,Pending,03-08-2016,1|#|1,13,20161022,200500000012016,10,Pending,12-09-2016,1|#|1,13,20161129,200500000012016,10,Pending,22-10-2016,1|#|1,13,20170110,200500000012016,10,Pending,29-11-2016,1|#|1,13,20170215,200500000012016,10,Pending,10-01-2017,1|#|15-02-2017 APPEARANCE"
regular expression: /(.)*?01-02-2016(.)*?\|\#\|/igm
By using this regular expression i can able to delete unnecessary part in string.
Now i want to change 03-08-3016 (date) dynamically. If i use
var date = "01-02-2016"
var reg = /(.)*?${date}(.)*?\|\#\|/igm;
If you pring reg in console.log you will get like this below
console.log(reg) ----> output: '/(.)?01-02-2016(.)?|#|/igm'
Expected Final output will delete upto 01-02-2016,1|#|
Use this.
var regex="(.)*?01-02-2016(.)*?\\|\\#\\|";
var rx=new RegExp(regex,"igm");
console.log(rx);
//Then when do you want to change,
regex=regex.replace("01-02-2016","03-02-2016");
rx=new RegExp(regex,"igm");
console.log(rx);
JavaScript have 2 methods to make a Regular Expression.
1. write it in slashes //
2. Make from string using new RexExp(string);
If you make it from string, you can give the constraint(" global, incase, etc.") as the second parameter as i did in the above.
and also you have to double escape (\) the escape characters.

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