簡體   English   中英

如何在第一個“(”

[英]How to javascript split on first “(”

所以我想首先分割我的變量(在變量中。我的代碼目前看起來像這樣,但是不起作用。

var word = "pc and (laptop or test)";
var split = word.split(/((.+)/);

'split'的值= [ '', 'pc and (communication or (test and test2))', '' ]

所需結果: ['pc and ', ' communication or (test and test2))']

最簡單的方法是獲取字符的索引並對字符串進行切片。

 var word = "pc and (laptop or test) (or something other)", index = word.indexOf('('), split = index == -1 ? [word] : [word.slice(0, index), word.slice(index + 1)] console.log(split); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM