非常努力地嘗試正則運算式,但無法獲得所需的結果。
- 必須以數字開頭,最多為 4
- 后跟只有一個允許的字母表
請看下面的代碼。
var strings = [
'1h', //should match
'13s', //should match
'30m', //should match
'42hr', //should not match
'8 hours ', //should not match
'765765', //should not match
'5sec', //should not match
'23445345345s', //should not match
'335m' //should match
];
for (var i = 0; i < strings.length; i )
{
var match = strings[i].match(/\d{4}[h|m|s]{1}/g);
console.log(strings[i], (match ? "Match" : "Not match"));
}
uj5u.com熱心網友回復:
你的正則運算式應該是:
/^\d{1,4}[hms]$/gm
首先,您需要 4 位數字 d{4}
將其更改d{1,4}為一到四位數
然后加一個a$來表示字串的結尾,這樣字母后面就不允許有更多的字符了
查看Regex101,對于測驗和理解正則運算式非常有用
uj5u.com熱心網友回復:
我添加了一個 ^ 以從一開始就匹配
strings.filter(x => x.match(/^\d{1,4}[hms](?!\w)/g) )
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/391353.html
標籤:javascript 节点.js 正则表达式
上一篇:如何將單選按鈕的行內onClick功能更改為單獨的腳本
下一篇:瀏覽器認為HTML檔案是js檔案
