Hem*_*dav 3 javascript regex pattern-matching
我不知道为什么这个简单的代码不起作用。我计划将字符串与允许的模式进行匹配。该字符串只能包含a-z, A-Z, 0-9, _(下划线), .(点), -(hiphen)。
下面是代码:
var profileIDPattern = /[a-zA-Z0-9_.-]./;
var str = 'Heman%t';
console.log('hemant',profileIDPattern.test(str));
Run Code Online (Sandbox Code Playgroud)
代码为以下字符串记录“true”,尽管这些字符串与模式不匹配。
'Heman%t' -> true
'#Hemant$' -> true
Run Code Online (Sandbox Code Playgroud)
我不知道是什么问题。
尝试将其更改为以下 RegExp ( /^[a-zA-Z0-9_.-]*$/):
var profileIDPattern = /^[a-zA-Z0-9_.-]*$/;
var str1 = 'Hemant-._67%'
var str2 = 'Hemant-._67';
console.log('hemant1',profileIDPattern.test(str1));
console.log('hemant2',profileIDPattern.test(str2));Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3047 次 |
| 最近记录: |