![](/img/trans.png)
[英]how to replace first and last quotes with two different chars in this string “{0}”
[英]How to check first 5 chars & last 5 chars of a string?
我試圖僅在字符串的前5個字符為數字且后5個字符為字母的情況下驗證字符串:
我嘗試過的
var userId = "12345abcde";
// Get First and Second Five Characters
var firstFive = userId.substring(0, 5);
var secondFive = userId.substring(5, 10);
// get Global Letter and Nummers
var aStr = /[a-z, A-Z]/g;
var aNum = /[0-9]/g;
var c = userId.match(aNum);
// Try firstFive first...
if (firstFive === c) {
alert('yes');
} else {
alert('nop');
}
這會提醒nop
。
這是因為firstFive
是字符串,而c
是對象嗎? 我的思想錯誤在哪里?
實時示例: http : //jsfiddle.net/xe71dd59/1/
有小費嗎? 提前致謝!
嘗試
/^[0-9]{5}.*[a-z]{5}$/i.test("12345abcde")
match
返回結果數組;如果未找到結果,則返回NULL。
var c = firstFive.match(aNum);
if(c!=null)
{
if(c.length==5)
{
alert("Yes");
}
}
嘗試這樣做:
var userId = "12345abcde"; var result = /^[0-9]{5}.*[az]{5}$/i.test(userId); if (result) { alert('yes'); }else{ alert('nop'); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.