0

I am having a small issue with placing a RegExp pattern inside a string, I have 2 patterns which are both really the same. The first doesn't work I presume due to the \d - is it being seen as an escape character?

            var pattern = '^.{1,5}-\d{1,5}$'; // Doesn't work


            var pattern = '^[a-zA-Z]{1,5}-[0-9]{1,5}$'; // Works

Is there anyway around this ? apart from replacing the \d with [0-9]?

Here is the extra code I am using

   var regex = new RegExp(pattern);

   var result = regex.test(value);

Thanks in advance

3
  • If you create a RegExp by string you need to escape the \ so you have to write '^.{1,5}-\\d{1,5}$', or use the /.../ like mplungjan suggests. Commented Sep 18, 2013 at 9:40
  • var pattern = '^.{1,5}-\\d{1,5}$'; -- note 2 backslashes. Commented Sep 18, 2013 at 9:41
  • I updated my answer to include an explanation. Commented Sep 18, 2013 at 10:03

2 Answers 2

2

As found in the documentation you have several different ways to create a RegExp

Regular expression literal,

var regex = /^.{1,5}-\d{1,5}$/;

Constructor function of the RegExp object

var regex = new RegExp("^.{1,5}-\\d{1,5}$");

since it is a string, you need to escape any \

Same for \w and other backslashed chars

The second version is mostly used if you have variables you need to add to the regexp

Sign up to request clarification or add additional context in comments.

4 Comments

yes, but I'd add an explanation why exactly new RegExp("\d") doesn't work.
Because it is a string. You need to escape \
Thanks, I guess I'm aware of that ;) but my point is that an explanation would be helpful for the OP and others with the same problem.
This is great, var regex = /^.{1,5}-\d{1,5}$/; .... so can anyone explain what the / at start and finish means ? is it similar to ' but doesn't support escaping which is why you don't need the double \\d ?? Anyone have any resource where i can read about that, its the first time i have seem it
1

If you want the way your are writing the regex to work, you can double escape the d:

var pattern = '^.{1,5}-\\d{1,5}$'; // Should work
var regex = new RegExp(pattern);

Otherwise, you can use the regex directly using the delimiters /:

var pattern = /^.{1,5}-\d{1,5}$/;

In the first instance, you are storing the pattern in a string, and the actual characters that are being passed to the variables are: ^.{1,5}-\d{1,5}$ because \d has no meaning in a string, but \\d is a backslash and a literal d. You can try putting a backslash in a string:

console.log('\'); // Won't run
console.log(' \ '); // Returns a space
console.log('\n'); // Returns a newline character

So that if you mean a literal backslash, you have to escape it.

Using:

var pattern = '^.{1,5}-\\d{1,5}$'; // Should work
var regex = new RegExp(pattern);

should be faster though, if you are using the regex several times, because here, you are compiling the regex so that you can use it multiple times.

The other way will require compiling the regex each time it is called for.

4 Comments

Is the \d also slower in JS?
In javascript, \d == [0-9]
@thg435 Thanks. I just read that JS's regex doesn't support the unicode characters, which would indeed mean \d == [0-9] (and I thought it could handle it...)
@mplungjan No sorry, it was a mistake. But if it wasn't, I was mainly pointing at the other possible matches besides digit characters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.