ASP.NET - Passing regular expression pattern through parameter , but its not working

Asked By Sam Sam on 07-Jun-13 07:48 AM

function IsPulseSerialValid(value,regexp)

{

var regexpval = regexp;

var serial = value;

if(serial.match(regexpval))

alert('hi');

else

alert('hello');

}



Passing value and regular expression pattern.But may its because of its considering it string or else.

If i declare it as var regexpval=/^([a-zA-Z0-9]){12,25}$/  then its working fine.
but for var regexpval = regexp;  //this is not working.

May be because of its considering like this  var regexpval="/^([a-zA-Z0-9]){12,25}$/"
Robbe Morris replied to Sam Sam on 07-Jun-13 09:55 AM
Why is this necessary?

var regexpval = regexp;
var serial = value;

Couldn't you just do

if (value.match(regexp)) blah blah blah

and

Are you sure regexp has the value you are expecting to be passed in?  Perhaps you are calling the function improperly
Sam Sam replied to Robbe Morris on 07-Jun-13 10:16 AM
No i know its the same thing , but if i use like this if (value.match(regexp))
then also it wont work.
Robbe Morris replied to Sam Sam on 07-Jun-13 10:28 AM

I've never tried to execute "match" off a string variable to execute a regex.  So, not sure what is up:

function URLDecode(encodedString)
{
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
 
  try
  {
    while ((match = myregexp.exec(output)) != null
       && match.length > 1
       && match[1] != '')
    {
      binVal = parseInt(match[1].substr(1), 16);
      thisString = String.fromCharCode(binVal);
      output = output.replace(match[1], thisString);
    }
  }
  catch (e) { }
  return output;
}