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.

3
  • possible duplicate of Converting user input string to regular expression Commented Mar 27, 2014 at 15:56
  • @TheHippo it is different scenario i am using this in angular Commented Mar 27, 2014 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. Commented Mar 27, 2014 at 16:12

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

3 Comments

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
@Karl-André Gagnon i am getting same error object doesnot support property or method test
@Crayon Violent $scope.searchEstimateMeasures is fine for example purpose only i have given like that, the variable is working fine.

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.