JavaScript错误:"val.match不是函数" [英] Javascript error: "val.match is not a function"

查看:175
本文介绍了JavaScript错误:"val.match不是函数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将match函数用于正则表达式.

I used the match function for regular expression.

我使用的代码是

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

"val.match is not function"

我找不到问题所在,

预先感谢

推荐答案

我会说val不是字符串.

I would say that val is not a string.

对于以下内容,我收到"val.match is not function"错误

I get the "val.match is not function" error for the following

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

如果您明确转换为字符串String(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);
}

这篇关于JavaScript错误:"val.match不是函数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆