![](/img/trans.png)
[英]Javascript regular expression to check whether a string is a combination of certain character
[英]How to check whether a certain character in string comes after another character?
給定一個字符串aaabbbcccooeellmmzzz
,我如何計算 'm' 之后的字符數。 例如在這種情況下它應該返回 5(即 oozzz);
注意:這里 o 和 z 字符按字母順序出現在“m”之后,而不是它們的索引 position 在 m 之后。
所以我想計算字符(n,o,p,q,r,s,t,u,v,w,x,y,z)在字符串中出現的次數。
樣本輸入: aaabbbcccooeellmmmzzz
樣品 Output: 5
使用substring
刪除從開始到字符最后一次出現的索引的所有內容,然后獲取長度:
let character = 'm'; let str = 'aaabbbcccooeellmmmzzz'; let res = str.substring(str.lastIndexOf(character) + 1).length; console.log(res)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.