javascript在字符串开头检查特殊字符

Nom*_*mad 1 javascript string

我试图从输入字段中获取一个值。

我想检查字符串的开头是否有%或*,然后显示警报。

nnn*_*nnn 5

if (/^[%*]/.test(document.getElementById("yourelementid").value))
   alert("Where can I find a JavaScript tutorial?");
Run Code Online (Sandbox Code Playgroud)

作为解释:正则表达式/^[%*]/表示:

^       match the beginning of the string, followed immediately by
[%*]    any one of the characters inside the brackets
Run Code Online (Sandbox Code Playgroud)

针对该正则表达式测试输入的值将返回true或false。