开发者

jQuery match from value string

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜