2

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/

3
  • 3
    if(hash.match(new RegExp('!' + param + '(.*)|&' + param + '(.*)')) Commented Nov 21, 2013 at 12:14
  • yepp this works. thanks a lot!! Please post as answer to can accept Commented Nov 21, 2013 at 12:16
  • possible duplicate of Javascript Regexp dynamic generation? Commented Nov 21, 2013 at 12:27

1 Answer 1

1

The following will work for parameters in RegExp.

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.