startsWith()检查不同的字母或字符串

时间:2018-12-18 08:15:07

标签: javascript string typescript

有什么方法可以检查字符串是否以不同的字母开头,而不只是一种检查。

下面是我当前的代码:

   myString.startsWith('M') ||
   myString.startsWith('L') ||
   myString.startsWith('V') ||
   myString.startsWith('H')

我希望将其最小化,如下所示:

   myString.startsWith('M'|| "L" || "V" || "H") 

关于使用其他方法或适当代码的任何建议。

3 个答案:

答案 0 :(得分:4)

使用正则表达式:

select coalesce(t1.date, t2.date, t3.date) as date, t1.Opened, t2.Finished, t3.Closed,
coalesce(t1.location, t2.location, t3.location) as location
from
(SELECT Convert(date,DateOpen) as Date ,count(JobID) as Opened, location      
FROM JOBS
WHERE DateOpen BETWEEN '12/16/2018' AND DATEADD(DAY, 1, '12/17/2018')
group by Convert(date,DateOpen), location 
) t1
Full join 
(SELECT Convert(date,DateFinish) as Date ,count(JobID) as Finished, location      
FROM JOBS
WHERE DateFinish BETWEEN '12/16/2018' AND DATEADD(DAY, 1, '12/17/2018')
group by Convert(date,DateFinish), location 
) t2  ON(t1.date = t2.date and t1.location = t2.location)
FULL JOIN
(SELECT Convert(date,DateClose) as Date ,count(JobID) as Closed, location      
FROM JOBS
WHERE DateClose BETWEEN '12/16/2018' AND DATEADD(DAY, 1, '12/17/2018')
group by Convert(date,DateClose), location 
) t3  ON(t2.date = t3.date and t2.location = t3.location)

它更快,更容易阅读和维护。

答案 1 :(得分:2)

使用带有字符集的正则表达式代替:

const check = str => /^[MLVH]/.test(str);
console.log(check('Mmm'));
console.log(check('Lmm'));
console.log(check('mmm'));

答案 2 :(得分:0)

我这样做:

['M','L','V','H'].some(chr => myStr.startsWith(chr));

在我看来,它比正则表达式更简单易读。