我有一個字串:The way ^[quick] brown fox ^[jumps] over the lazy ^[dog].
我希望輸出為:[The way, ^[quick], brown fox, ^[jumps], over the lazy, ^[dog]].
嘗試使用正則運算式排除匹配的字串:/\^\[[^\]]*\]/g
但實際結果是:["The way ", " brown fox ", " over the lazy ", ""]
我錯過了什么?
這是鏈接:https ://codesandbox.io/s/still-platform-tofbrj?file=/src/index.js:73-104
uj5u.com熱心網友回復:
你錯過了()——/(\^\[[^\]]*\])/g
String.prototype.split匹配組中的元素而不是丟棄它們
const text = 'The way ^[quick] brown fox ^[jumps] over the lazy ^[dog]'
const result = text.split(/(\^\[[^\]]*\])/g)
console.log(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/533889.html
上一篇:如何從陣列元素中洗掉空字串
