0

I trying to match a variable from an url. This works fine if I use the expression directly in the match method. However I am having problem getting it to work if the expression is inside a string.

var match = '/(page_art_list=\d+)/';

match contains the value..

var pattern = "/("+paramName+"=\d+)/";
var match = this.href.match(pattern);

match is null

I have double checked that both examples produce exactly the same string.

Any thoughts?

Best regards. Asbjørn Morell

atmorell
  • 3,052
  • 6
  • 30
  • 44
  • See http://stackoverflow.com/questions/901115/get-querystring-values-with-jquery – rsp Feb 24 '11 at 12:40

1 Answers1

2

The /something/ syntax is for regexp literals. For strings, use the RegExp constructor:

var pattern = new RegExp('(' + paramName + '=\\d+)');

Note the double backslash \\. This is because within strings, \ is an escape character, so you need two to represent a single, regexp backslash.

David Tang
  • 92,262
  • 30
  • 167
  • 149