我需要一個regEx來匹配除@符號內的所有空白,以進行正確的分割。
我有這樣一個字串:
[0] == @Item 1 @
通過拆分,我需要下面的陣列(第3個元素中是否有@):
var array = ["[0]","==","@Item 1@"] 。
通過簡單的split(" "),我得到了這樣的結果:
。var array = [" [0]", "==","@Item","1@"】。]
謝謝你的幫助。
uj5u.com熱心網友回復:
你可以使用
const text = ' [0] == @Item 1@';
console.log( text.match(/(?:@[^@]*@|S) /g) )/code
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
請看regex演示。(?:@[^@]*@|S) 模式意味著:
(?:- 一個非捕獲組的開始。(? 。@[^@]*@- 一個@字符,0個或多個除@以外的字符,然后一個@字符|- or 。S- 任何非空白字符
)- 該組的結束,重復一次或多次。
/g平告訴.match()要提取所有的出現。
uj5u.com熱心網友回復:
我不知道你是否只需要陣列中的3個元素,如果是,你可以試試
string.split(" "/span>, 3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/323613.html
標籤:
