fefe
fefe

Reputation: 9055

using variable in jquery, javascript regexp

How to use variables inside regexp(to check the existence of a substring ) taking in consideration the following example

var hash = '!price=475;1500&ram=475;1275';
var uri_params = ['price', 'display', 'ram', 'hdd', 'brand'];
$.each(uri_params,function(index, param){
            console.log(param);
            if(/( !param(.*)|&param(.*))/i.test(hash)){
                 console.log('test');
                //than here I should add the new value to matched param

            }
});

JSFiddle http://jsfiddle.net/lgtsfiddler/c936b/8/

Upvotes: 2

Views: 42

Answers (1)

NoLifeKing
NoLifeKing

Reputation: 1939

The following will work for parameters in RegExp.

if(hash.match(new RegExp('!' + param + '(.*)|&' + param + '(.*)'))

Upvotes: 1

Related Questions