8

I have a html phone pattern that will accept these formats :

+61 x xxxx xxxx,
+61xxxxxxxxx,
0x xxxx xxxx,
0xxxxxxxxx,
xxxx xxxx,
xxxxxxxx,
+xx xxx xxx xxx,
+xxxxxxxxxxx,
0xxx xxx xxx,
0xxxxxxxxx

It was working few months ago, now suddenly my phone fields are not validating . I'm having this error:

Pattern attribute value ^(?:0|\(?\+61\)?\s?|0061\s?)[1-79](?:[\.\-\s]?\d\d){4}|(\d{4}[\s]\d{4})|(\d{8})|(\d{4}[\s]\d{3}[\s]\d{3})|(\+61\[\s]\d{3}[\s]\d{3}[\s]\d{3})|(\+61\s\d{3}\s\d{3}\s\d{3})$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^(?:0|\(?\+61\)?\s?|0061\s?)[1-79](?:[\.\-\s]?\d\d){4}|(\d{4}[\s]\d{4})|(\d{8})|(\d{4}[\s]\d{3}[\s]\d{3})|(\+61\[\s]\d{3}[\s]\d{3}[\s]\d{3})|(\+61\s\d{3}\s\d{3}\s\d{3})$/: Lone quantifier brackets 
6
  • 2
    That's exactly what it means. This regexp is not valid.
    – nicovank
    Commented Dec 2, 2016 at 19:04
  • Yeah, there is a definite problem with the regex, if you are not familiar with the semantics of building and testing regex expressions, I would strongly suggest using a tool like expresso (ultrapico.com/Expresso.htm) to build and test regex expressions. Commented Dec 2, 2016 at 19:11
  • Look at the visualization and you can clearly see it isn't valid - regexper.com/… Commented Dec 2, 2016 at 19:14
  • I tried to fix the pattern, please check this demo, does it work as expected? Commented Dec 2, 2016 at 19:23
  • @Phillip Copley - This visualization doesn't show at all that the expression at hand wouldn't be valid.
    – Armali
    Commented Mar 5, 2018 at 13:23

4 Answers 4

25

So far, no one cared to show where in your pattern the error is.

…|(\+61\[\s]\d{3}[\s]\d{3}[\s]\d{3})|(\+61\s\d{3}\s\d{3}\s\d{3})$
       ^

There by mistake you inserted a backslash, escaping the opening bracket, so making it an ordinary character and leaving the closing bracket Lone. (Sadly the error message is somewhat misleading, since those brackets are of course not quantifier brackets.)

2
  • 3
    That is an interesting problem, according to online tools like regex101.com or regexr.com this closing bracket should not be a special character outside of a character class. I wonder if that is a javascript bug.
    – findusl
    Commented Jul 22, 2021 at 20:48
  • 2
    By the way, I could now reproduce this error message only when using the u modifier.
    – Armali
    Commented Jul 22, 2021 at 21:44
3

Just to spell it out a bit more explicitly, the difference between VSCode (and apparently Visual Studio) and other Java/ECMAScript regex interpreters is that the VS products are picky about matching brackets, even if they should be interpreted as literal characters.

For example, the regex below - with just the first bracket escaped - is fine with matching a string surrounded by square brackets in most JS-based (and PCRE) regex interpreters. e.g.: [string]

\[\w+]
^

While just the "opening" square bracket is escaped, the "closing" square bracket is also interpreted as a literal character. Semantically, with the first bracket escaped, you haven't actually started a character class match (or group or quantifier, if it's parentheses () or curly braces {} you're matching).

In VSCode, you must also escape the "closing" bracket, or else you get the "Lone quantifier brackets" error.

\[\w+\]
^    ^

Another irritating aspect is that the .NET regex interpreter behaves very similarly to a standard Javascript interpreter, so it similarly does not care about escaping both brackets if the opening one is escaped. Something of this nature that's fine in .NET or Powershell won't work the same in VSCode.

1

Similar to the request above, I received the error in Visual Studio Code using the following RegEx to capture words wrapped in brackets:

\[([^\\[\r\n]*(?:\\.[^\\]\r\n]*)*)\] (which is incorrect)

on the text string (SQL)

SELECT d.[fname]
,d.[lname],d.[address1],d.[address2],d.[City],d.[zip]
FROM my_table d
WHERE [hh_id] IN (SELECT [HhId]
FROM other_table)

However using this RegEx pattern in online regex tools shown in https://regex101.com/r/HGycR9/1 show it is valid RegEx.

The correct RegEx pattern is \[([^[\r\n]*(?:\\.[^\r\n]*)*)\], which will highlight:

SELECT d.[fname] ,d.[lname],d.[address1],d.[address2],d.[City],d.[zip] FROM my_table d WHERE [hh_id] IN (SELECT [HhId] FROM other_table)

Then you can use $1 in the replace field of VS Code to remove the brackets and leave the words contained within them.

SELECT d.fname
,d.lname,d.address1,d.address2,d.City,d.zip
FROM my_table d
WHERE hh_id IN (SELECT HhId
FROM other_table)
2
  • I'm unclear which part of the original REGEX was considered invalid
    – Stevoisiak
    Commented Jan 6, 2023 at 19:01
  • I've added another answer that spells it out a bit more explicitly. It seems that VSCode/Visual Studio want both "opening" and "closing" brackets to be escaped if they are to be interpreted as literal characters, while other Javascript regex engines will interpret both as char literals if only the opening bracket is escaped.
    – LeeM
    Commented Jan 24, 2023 at 23:22
-1

That means exactly that, pattern invalid.

If you want to match phones from Australia, you could use:

pattern="^(?:0|\(?\+61\)?\s?|0061\s?)[1-79](?:[\.\-\s]?\d\d){4}$"

Pattern found here.

Example: https://jsfiddle.net

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.