我有一個輸入回傳給我兩種型別的值,我想檢查這個值是代碼還是文本
代碼編號模式>“847707/926503”文本模式>“omar atef”
我如何構建這樣的功能
checkValue("847707/926503") // return true
checkValue("omar atef") // return false
uj5u.com熱心網友回復:
你想要那樣的東西嗎?
function checkValue(input) {
return /^(\d{6})\/(\d{6})$/.test(input) ? true : /[A-Z]/ig.test(input) ? false : null;
}
uj5u.com熱心網友回復:
match 字串對照正則運算式,檢查字串是否是由斜杠分隔的一對六個數字。
const regex = /^(\d{6})\/(\d{6})$/;
function checkValue(str) {
const match = str.match(regex);
if (match) return `${str}: true`;
return `${str}: false`;
}
console.log(checkValue('847707/926503'));
console.log(checkValue(''));
console.log(checkValue('omar atef'));
console.log(checkValue('billy/12345'))
console.log(checkValue('988765/123456'));
console.log(checkValue('847707//926503'));
console.log(checkValue('847707/926503/'));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/339792.html
標籤:javascript
上一篇:React:隱藏和顯示使用map()顯示的陣列中的特定元素
下一篇:我想點擊一個單選按鈕
