Regex - Invalid Quantifier - javascript

I am using this regex in JavaScript which is to evaluate if a given string matches some german phone number patterns.
var reg = new RegExp("(?:\+\d+)?\s*(?:\(\d+\)\s*(?:[/–-]\s*)?)?\d+(?:\s*(?:[\s/–-]\s*)?\d+)*");
When using it I get this error:
SyntaxError: invalid quantifier
...eg = new RegExp("(?:\+\d+)\s*(?:\(\d+\)\s*(?:[/–-]\s*))\d+(?:\s*(?:[\s/–-]\s*)\d...
I'm trying hard to learn reading regular expressions, but can not understand them full yet. I did not write this expression by myself and I am struggling to understand it.
Why I am getting this error?

Because you're using a string litteral, you need to escape each backslash:
(?:\\+\\d+)?\\s*(?:\\(\\d+\\)\\s*(?:[/–-]\\s*)?)?\\d+(?:\\s*(?:[\\s/–-]\\s*)?\\d+)*
The other solution would be to use the regex litteral:
var reg = /(?:\+\d+)?\s*(?:\(\d+\)\s*(?:[\/–-]\s*)?)?\d+(?:\s*(?:[\s\/–-]\s*)?\d+)*/;

new RegExp requires a string, and since backslashes already have meaning inside strings, they need to be escaped again.
In your case, though, you're using a static pattern, so you'd be better off with a literal:
var reg = /(?:\+\d+)?\s*(?:\(\d+\)\s*(?:[\/–-]\s*)?)?\d+(?:\s*(?:[\s\/–-]\s*)?\d+)*/;
Just be aware that you need to escape / here ;)
As an additional tip, you can simplify the need to escape things to some extent by doing stuff like [+] for a literal +. I think it looks nicer then \+, but that's just my opinion.

Related

Regex not working as expected in JavaScript

I wrote the following regex:
(https?:\/\/)?([da-z\.-]+)\.([a-z]{2,6})(\/(\w|-)*)*\/?
Its behaviour can be seen here: http://gskinner.com/RegExr/?34b8m
I wrote the following JavaScript code:
var urlexp = new RegExp(
'^(https?:\/\/)?([da-z\.-]+)\.([a-z]{2,6})(\/(\w|-)*)*\/?$', 'gi'
);
document.write(urlexp.test("blaaa"))
And it returns true even though the regex was supposed to not allow single words as valid.
What am I doing wrong?
Your problem is that JavaScript is viewing all your escape sequences as escapes for the string. So your regex goes to memory looking like this:
^(https?://)?([da-z.-]+).([a-z]{2,6})(/(w|-)*)*/?$
Which you may notice causes a problem in the middle when what you thought was a literal period turns into a regular expressions wildcard. You can solve this in a couple ways. Using the forward slash regular expression syntax JavaScript provides:
var urlexp = /^(https?:\/\/)?([da-z\.-]+)\.([a-z]{2,6})(\/(\w|-)*)*\/?$/gi
Or by escaping your backslashes (and not your forward slashes, as you had been doing - that's exclusively for when you're using /regex/mod notation, just like you don't have to escape your single quotes in a double quoted string and vice versa):
var urlexp = new RegExp('^(https?://)?([da-z.-]+)\\.([a-z]{2,6})(/(\\w|-)*)*/?$', 'gi')
Please note the double backslash before the w - also necessary for matching word characters.
A couple notes on your regular expression itself:
[da-z.-]
d is contained in the a-z range. Unless you meant \d? In that case, the slash is important.
(/(\w|-)*)*/?
My own misgivings about the nested Kleene stars aside, you can whittle that alternation down into a character class, and drop the terminating /? entirely, as a trailing slash will be match by the group as you've given it. I'd rewrite as:
(/[\w-]*)*
Though, maybe you'd just like to catch non space characters?
(/[^/\s]*)*
Anyway, modified this way your regular expression winds up looking more like:
^(https?://)?([\da-z.-]+)\.([a-z]{2,6})(/[\w-]*)*$
Remember, if you're going to use string notation: Double EVERY backslash. If you're going to use native /regex/mod notation (which I highly recommend), escape your forward slashes.

Issue with custom javascript regex

I have a custom regular expression which I use to detect whole numbers, fractions and floats.
var regEx = new RegExp("^((^[1-9]|(0\.)|(\.))([0-9]+)?((\s|\.)[0-9]+(/[0-9])?)?)$");
var quantity = 'd';
var matched = quantity.match(regEx);
alert(matched);
​
(The code is also found here: http://jsfiddle.net/aNb3L/ .)
The problem is that for a single letter it matches, and I can't figure out why. But for more letters it fails(which is good).
Disclaimer: I am new to regular expressions, although in http://gskinner.com/RegExr/ it doesn't match a single letter
It's easier to use straight regular expression syntax:
var regEx = /^((^[1-9]|(0\.)|(\.))([0-9]+)?((\s|\.)[0-9]+(\/[0-9])?)?)$/;
When you use the RegExp constructor, you have to double-up on the backslashes. As it is, your code only has single backslashes, so the \. subexpressions are being treated as . — and that's how single non-digit characters are slipping through.
Thus yours would also work this way:
var regEx = new RegExp("^((^[1-9]|(0\\.)|(\\.))([0-9]+)?((\\s|\\.)[0-9]+(/[0-9])?)?)$");
This happens because the string syntax also uses backslash as a quoting mechanism. When your regular expression is first parsed as a string constant, those backslashes are stripped out if you don't double them. When the string is then passed to the regular expression parser, they're gone.
The only time you really need to use the RegExp constructor is when you're building up the regular expression dynamically or when it's delivered to your code via JSON or something.
Well, for a whole number this would be your regex:
/^(0|[1-9]\d*)$/
Then you have to account for the possibility of a float:
/^(0|[1-9]\d*)(.\d+)?$/
Then you have to account for the possibility of a fraction:
/^(0|[1-9]\d*)((.\d+)|(\/[1-9]\d*)?$/
To me this regex is much easier to read than your original, but it's up to you of course.

Why cant I get my Regular expression working?

What am I doing wrong as both strings below are returning false when tested below?
var pattern = "^[\s\da-zA-ZåäöÅÄÖ_]+$"
var reg = new RegExp(pattern);
console.log(reg.test("This should be invalid as it is full with invalid chars. #!¤%&/()=?"));
console.log(reg.test("This is an valid string, despite that Swedish chars such as ÅÄÖ are used"));
You need to double-up on the backslashes in the pattern.
var pattern = "^[\\s\\da-zA-ZåäöÅÄÖ_]+$"
The problem is that when you build regular expression objects that way, there are two passes made over the string: one to interpret it as a string, and then a second to interpret it as a regular expression. Both of those micro-syntaxes use \ to mean something, so by doubling them you get a single backslash out of the string constant parse.
If your pattern is really a constant, and not something that you construct dynamically from separate parts, then you can just use the native syntax for regular expressions:
var pattern = /^[\s\da-zA-ZåäöÅÄÖ_]+$/;
Only one backslash is necessary because the pattern is only parsed once, as a regular expression.

JS Regex with variables and possible illegal chars

I cannot get this regex string to work in Javascript:
var input = $("input").val();
var hi = "(?<=[^ ​ ‍])" + input + "(?=[$ ​ ‍])";
var reg = new RegExp(hi);
alert(reg);
The last line is not working, but it does work when the regex is valid. I put the variable into a second string for the full regex search before passing that one to the regex object. Why isn't this regex query valid? (In case you are wondering, the chars in the brackets are space, zwsp, nbsp, and zwj.)
JavaScript regular expressions do not support look-behind.
They do however, support look-ahead, so if you really need the functionality you can reverse the input and write the expression "backwards". If you want both look-ahead and look-behind at the same time, this gets a little complicated.
As you haven't revealed what you're actually trying to achieve, you may be able to avoid the zero-width matches and just use normal capture groups.

javascript \d regular expression unexpected behavior

I am trying use javascript regular expressions to do some matching and I found a really unusual behavior that I was hoping someone could explain.
The string I was trying to match was: " 0 (IR) " and the code block was
finalRegEx = new RegExp("[0-9]");
match = finalRegEx.exec(str);
except that when I put "\d" instead of "[0-9]" it didn't find a match. I'm really confused by this.
If you use RegExp with "\d" to build the regular expression, the "\d" will result in just "d". Either use two back slashes to escape the slash like "\\d" or simply use the regular expression literals /…/ instead:
match = /\d/.exec(str)
You need to escape it because you're using the constructor, otherwise it matches d literally:
new RegExp('\\d').test('1')
new RegExp should only be used for dynamic matching. Otherwise use a literal:
var foo = /\d/;
foo.test(1)
You probably need to escape the backslash: finalRegEx = new RegExp("\\d");

Categories

Resources