6

I am using a very fine JavaScript library called "array-query" by Jacob Wright to do searches in arrays of objects.

One method is regex() where a regular expression can be included in parentheses like this: regex(/[^\w\s]/). If I hardcode the expression as I just showed it works fine. If I put the same expression in a variable first it does not work, like this:

var reg = "/[^\w\s]/"; 
regex(reg);

I was told

You are putting quotes around your regex, making it a string. Remove the quotes.

Thus

var reg = /[^\w\s]/; 
regex(reg);

works fine.

Problem is I need to accept the user input from an textbox as part of the regular expression. For example if the user types in the letter z it needs to get changed to /z/. Even if I type in /z/ the textbox.value returned has the same problem as a var reg = "/z/". If I hardcode var reg = /z/; regex(reg); it works fine.

How to make a input textbox value of "z" into a form that is var reg = z;? Many many thanks for any help or ideas, hope this isn't too confusing.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
user2481168
  • 71
  • 1
  • 1
  • 5
  • 3
    Here: http://stackoverflow.com/questions/874709/converting-user-input-string-to-regular-expression – mzedeler Jun 13 '13 at 07:49
  • This as been asked before, have a look at the link mzedeler posted. I also recommend to read some documentation about regular expressions in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions. – Felix Kling Jun 13 '13 at 07:54

3 Answers3

4

You should do

var regex = new RegExp('your regex string');

Basically you can think of

var regex = /pattern/modifiers;

as

var regex = new RegExp(pattern,modifiers);

Read more about it at: MDN or w3schools

Matyas
  • 13,473
  • 3
  • 60
  • 73
2
var reg = new RegExp("string");
Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
Thelambofgoat
  • 609
  • 1
  • 11
  • 24
-3

You can do something like this:

var string = $('#input_id').val();
string = string.replace('/', '');

var regexpPattern = '/'+string+'/';
regex(regexpPattern);
skparwal
  • 1,086
  • 6
  • 15
  • 2
    `regex` expects a RegExp object, not a string. – Fabrício Matté Jun 13 '13 at 08:16
  • 1
    That's very wrong. `"/foo/"` is a string, while `/foo/` is a regex literal. The OP even mentioned that in the question, have you read it? – Felix Kling Jun 13 '13 at 09:09
  • I haven't include any double quotes. I have just concatinate the slashes with the text that are captured via input box. Have you read my code carefully? – skparwal Jun 13 '13 at 09:12
  • you can change it to RegExp object like: var reg = new RegExp('/'+string+'/'); – skparwal Jun 13 '13 at 09:14
  • It doesn't matter whether you use single or double quotes. Both denote a string. I could also have written `'/foo/'` if you like that better. Yes, passing the value to `RegExp` is necessary here and that's the actual solution! **But** including the slashes is wrong. The correct solution would be `regex(new RegExp(string))`. Otherwise the regex would only match if the value literally contains `/`. – Felix Kling Jun 13 '13 at 09:23
  • Wow!! Thank you all sooooo much for such fast and great answers. It works!!! var reg = new RegExp(string); I can breath again. Markandeya – user2481168 Jun 13 '13 at 11:21
  • @skparwal maybe you wanted to write something like this `var _str = "sample string"; _str = "/"+_str+"/s"; var _reg = new RegExp (_str); ` – w411 3 Mar 07 '17 at 20:48