![](/img/trans.png)
[英]How to get particular part of string from an array of strings using javascript?
[英]How to get the number after particular string from array of strings using javascript?
下面是一個字符串數組。
const arr = [
"list/1/item/1/",
"item/2/",
"item/3/subitem/2/",
"item/4/subitem/3/someother/5",
]
聽起來您想要
item\/<any_number><\/code> ,但前提是它是字符串中的最后一件事。
為此,您將使用
\/\\bitem\\\/\\d+\\\/$\/<\/code> :
\\b<\/code>表示“ subitem<\/code>詞”,所以我們不匹配子項
<\/li>
item<\/code>從字面上匹配單詞 item
<\/li>
\\\/<\/code>是您在正則表達式中編寫\/<\/code>的方式,因此您匹配\/<\/code>字面意思
<\/li>
\\d<\/code>表示“任何數字”
<\/li>
+<\/code>表示“一個或多個(這些數字中的一個)”
<\/li>
\\\/<\/code>是另一個文字\/<\/code>
<\/li>
$<\/code>表示“輸入結束
<\/li><\/ul>
const arr = [ "list\/1\/item\/1\/", "item\/2\/", "item\/3\/subitem\/2\/", "item\/4\/subitem\/3\/someother\/5", ]; const rex = \/\\bitem\\\/\\d+\\\/$\/; const output = arr.filter(str => rex.test(str)); console.log(output);<\/code><\/pre>
"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.