0

i want to concatenate string with regular expression

my regular expression is

$scope.searchEstimateMeasures = Ne;
 var exp = new RegExp("/^" + $scope.searchEstimateMeasures +"/i");

when i am trying to test this regualr expression using

if (exp.test(Net Value))

i am getting an error saying object doesnot support property or method test

i tried using

var exp = new RegExp('/^' + $scope.searchEstimateMeasures +'/i');

but it is returning false.

if i use regex directly(/^ne/i.test(Net Value)) i am getting true.

TheHippo
  • 61,720
  • 15
  • 75
  • 100
nLearn
  • 55
  • 1
  • 5
  • possible duplicate of [Converting user input string to regular expression](http://stackoverflow.com/questions/874709/converting-user-input-string-to-regular-expression) – TheHippo Mar 27 '14 at 15:56
  • @TheHippo it is different scenario i am using this in angular – nLearn Mar 27 '14 at 16:10
  • No its no difference there. Its a string that you want to transform into a regular expression. It does not matter where this string is coming from. – TheHippo Mar 27 '14 at 16:12

1 Answers1

3

new RegExp accept 2 arguments.

The first one is what's usualy be the / /.

The second one is the flag.

Try that :

var exp = new RegExp('^' + $scope.searchEstimateMeasures, 'i');
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
  • I certainly agree that OP needs to do these things, however, this is not the cause of his issue (though it certainly will be an issue once he sorts out his immediate issue). The 2nd arg to `RegExp` is not required, and while his pattern as-is certainly isn't what he wants, it's still *a* pattern, assuming the `RegExp` object instantiated properly in the first place. Which it sounds like it didn't, probably because of an issue with `$scope.searchEstimateMeasures` – CrayonViolent Mar 27 '14 at 16:05
  • @Karl-André Gagnon i am getting same error object doesnot support property or method test – nLearn Mar 27 '14 at 16:07
  • @Crayon Violent $scope.searchEstimateMeasures is fine for example purpose only i have given like that, the variable is working fine. – nLearn Mar 27 '14 at 16:09