59

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?

RobC
  • 22,977
  • 20
  • 73
  • 80
zahir
  • 591
  • 1
  • 5
  • 5
  • 3
    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)`. – Reid Feb 03 '11 at 05:07
  • the val is string, just now i checking the match is working for some word and not working the some word, – zahir Feb 03 '11 at 05: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*$)" – Eric Wendelin Feb 03 '11 at 05: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 – zahir Feb 03 '11 at 05:50
  • No trouble at all, and you're welcome :) – Eric Wendelin Feb 09 '11 at 21:42
  • 2
    Seems like this has been answered - would be nice to see Eric get the credit. Care to add it as an answer? – johnhunter Feb 25 '11 at 11:37
  • @Eric: That should have been an answer, not a comment. – Lightness Races in Orbit Mar 04 '11 at 17:32
  • Agreed, Tomalak. Long as problem is solved all is generally good, though :) – Eric Wendelin Mar 11 '11 at 18:48

3 Answers3

82

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);
}
Rifky Niyas
  • 1,737
  • 10
  • 25
chrisp7575
  • 853
  • 5
  • 3
  • Thanks! I was using the code at https://stackoverflow.com/questions/881085/count-the-number-of-occurrences-of-a-character-in-a-string-in-javascript and was getting the "is not a function" message and was going crazy! – Mark Stewart Mar 13 '20 at 16:21
  • 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 – robivictor Apr 01 '21 at 05:29
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);
}
skipper
  • 271
  • 2
  • 4
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*$)"

Eric Wendelin
  • 43,147
  • 9
  • 68
  • 92
  • 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. – Benjiro Jan 06 '22 at 17:48