[英]Regex: How to create an array of objects from all matches in javascript?
[英]Create array of regex matches javascript
我想在 javascript 中為我的正則表達式創建一個匹配數組。
它應該看起來像: ['<:S_egirl:1023714296590499882>', '<a:eee:1023672889490284606>']
但是,我的代碼目前只創建一個包含所有匹配項的數組。
表達式為:/<:.+:(\d+)>/ /<:.+:(\d+)>/gm
我的代碼是const parsed_emotes = [].concat(emotes.match(/<:.+:(\d+)>/gm)); console.log(parsed_emotes);
const parsed_emotes = [].concat(emotes.match(/<:.+:(\d+)>/gm)); console.log(parsed_emotes);
我已經堅持了幾個小時,感謝您的幫助。
用 .+ 讓你的.+
不貪心?
. 比較這兩個匹配的 output:
'<:hello:1234> <:hello:1234>'.match(/<:.+:(\d+)>/gm)
// Produces ['<:hello:1234> <:hello:1234>']
與非貪婪匹配:
'<:hello:1234> <:hello:1234>'.match(/<:.+?:(\d+)>/gm)
// Produces ['<:hello:1234>', '<:hello:1234>']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.