61

I’m using the match function with a Regular Expression.

The code I’m using is:

if(val.match(/^s+$/) || val == "" )

However, it produces the following error:

"val.match is not function"

What is the problem?

8
  • 4
    Can we see where you have defined val? Make sure val is a string: val.toString(). Or you can use Regex.exec to implicitly convert to a string: /^s+$/.exec(val). Commented Feb 3, 2011 at 5:07
  • the val is string, just now i checking the match is working for some word and not working the some word, Commented Feb 3, 2011 at 5:19
  • 3
    Definitely make sure val is defined and a String. Also, I'm guessing it's a typo that you don't have a slash before the 's' in your regex. If that is the case you can replace your if test with "if(val.match(/^\s*$)" Commented Feb 3, 2011 at 5:35
  • 1
    Thanks for the idea Mr. Eric Wendelin, I just changed the coding like to val.toString(), that is working, i am new to javascript, if any drouble for ur work, sorry, thanks eric Commented Feb 3, 2011 at 5:50
  • 2
    Seems like this has been answered - would be nice to see Eric get the credit. Care to add it as an answer? Commented Feb 25, 2011 at 11:37

3 Answers 3

90

I would say that val is not a string.

I get the

val.match is not function

error for the following

var val=12; 
if(val.match(/^s+$/) || val == ""){
   document.write("success: " + val);
}

The error goes away if you explicitly convert to a string String(val)

var val=12; 
if(String(val).match(/^s+$/) || val == ""){
   document.write("success: " + val);
}

And if you do use a string you don't need to do the conversion

var val="sss"; 
if(val.match(/^s+$/) || val == ""){
   document.write("success: " + val);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I was using the code at stackoverflow.com/questions/881085/… and was getting the "is not a function" message and was going crazy!
if you need this to implement some sort of pipe transform, it is a good idea to convert the incoming values to string to make sure it works all the time. Thanks @chrisp7575
23

the problem is: val is not string

i can think of two options 1) convert to string: might be a good option if you are sure val has to be string

"Same as above answer"

var val=12; 
if(String(val).match(/^s+$/) || val == ""){
   document.write("success: " + val);
}

2) skip the line: in my case, it was better to just check the val type and skip if it is not string, because it was not a good idea to run "match" function anyways.

val = 12;
if( val.match) {
  if(val.match(/^s+$/) || val == "" ) {
    document.write("success: " + val);
  }
} else {
    document.write("not a string: " + val);
}

Comments

5

NOTE: making this an answer as suggested above from my comment.

Definitely make sure val is defined and a String. Also, I'm guessing it's a typo that you don't have a slash before the 's' in your regex. If that is the case you can replace your if test with "if(val.match(/^\s*$)"

1 Comment

I have a similar issue, in my case, I am validating that the value is a number and not an alphabet, will converting it to string be a good idea? because when I do, the validation fails and returns an object instead of either string(error message) or boolean.

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.