Lee
Lee

Reputation: 273

javascript regex is giving error of illegal characters

I want to use match() to look for a pattern. Here is an example of the string i want to match: 12/03/2013 11:15

i used a few online tools and i got it working on those with this pattern:

sData.match((0[1-9]|[12][\d]|3[0-2])\/(0[1-9]|1[0-2])\/[\d]{4} (0[1-9]|1[\d]|2[0-3]):(0[1-9]|[1-5][\d])$)

However once i used it within my javascript code i get an error of illegal characters but ive not idea which characters are illegal can anyone help?

If it helps this is for a custom sort column for the datatables plugin but im sure this is not part of the problem.

Here is the online tool with the working regex: http://rubular.com/r/PR4l6T8AQi

Upvotes: 0

Views: 1249

Answers (3)

gp.
gp.

Reputation: 8225

regex must start with / and end with /

 sData.match(/(0[1-9]|[12][\d]|3[0-2])\/(0[1-9]|1[0-2])\/[\d]{4} (0[1-9]|1[\d]|2[0-3]):(0[1-9]|[1-5][\d])$/)

Upvotes: 0

Jerry
Jerry

Reputation: 71578

You just need to add the delimiters for the regex:

sData.match(/(0[1-9]|[12][\d]|3[0-2])\/(0[1-9]|1[0-2])\/[\d]{4} (0[1-9]|1[\d]|2[0-3]):(0[1-9]|[1-5][\d])$/);
            ^                                                                                            ^

That should work :)

Also, you can drop some of the character classes, such as [\d] can be written as \d all right:

sData.match(/(0[1-9]|[12]\d|3[0-2])\/(0[1-9]|1[0-2])\/\d{4} (0[1-9]|1\d|2[0-3]):(0[1-9]|[1-5]\d)$/);

Upvotes: 4

Tibos
Tibos

Reputation: 27833

A literal regexp should start and end with /

Upvotes: 2

Related Questions