Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this a valid regular expression?

This was just working a day ago but now throwing exception. I've pasted regex into online verifier and it looks good?

What am I missing?

<input type="text" class="form-control" name="nickname" id="nickname" value="Alicia"
       pattern="[\w\s\-\(\)\!\,\.]{3,45}">

Pattern attribute value [\w\s\-\(\)\!\,\.]{3,45} is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /[\w\s\-\(\)\!\,\.]{3,45}/: Invalid escape

Thanks for the help!

Casey

like image 439
cph Avatar asked Sep 21 '16 23:09

cph


People also ask

How do I check if a regular expression is valid?

The static Regex. Match method returns a single Match object. By using this static method to run a regular expression against a string (in this case a blank string), we can determine whether the regular expression is invalid by watching for a thrown exception.

What can regular expressions not do?

In short regular expressions does not allow the pattern to refer to itself. You cannot say: at this point in the syntax match the whole pattern again. To put it another way, regular expressions only matches linearly, it does not contain a stack which would allow it to keep track of how deep it is an a nested pattern.

How do you identify a regular expression?

A regex (regular expression) consists of a sequence of sub-expressions. In this example, [0-9] and + . The [...] , known as character class (or bracket list), encloses a list of characters. It matches any SINGLE character in the list.

What are the rules for regular expression?

Inductive Rule 1: If A and B are regular expressions, then A B matches concatenations of A and B . Inductive Rule 2: If A and B are regular expressions, then A | B is a regular expression that matches stands matched by either A or B .


1 Answers

Looks valid to me though Chrome does indeed complain.

In any case, you don't need to escape all those characters within a character class. You should be able to use

pattern="[\w\s()!,.-]{3,45}"

which seems to work ok for me.

like image 76
Phil Avatar answered Oct 11 '22 13:10

Phil