2017-03-29 22 views
-4

早上好,分裂沒有一些空間

如果我有一個字符串:

"@Hello(first second), one two" 

而且我想一個數組:

["@Hello(first second)", "one", "two"] 

我怎麼能做到這一點?

感謝,

+2

你已經試過了什麼? – BenM

+0

通過什麼邏輯你將該字符串轉換成該數組?如果你要告訴某人如何去做,你會採取哪些措施?這些步驟將是您用於自動執行該過程的代碼的起點。 – David

+0

「沒有相同的空格」是什麼意思?它們是什麼?你的意思是「沒有**一些空格」嗎? – Barmar

回答

0

您可以使用.split()RegExp/\s(?!\w+\))/匹配空格字符後面沒有字字符後跟右括號「)」

var str = "@Hello(first second), one two"; 
var res = str.split(/\s(?!\w+\))/); 
console.log(res); 

或者您可以使用.match()RegExp/@\w+\(\w+\s\w+(?=\))\)|\w+(?=\s|$)/g匹配「@」後跟一個或多個單詞字符,後跟「(」後跟單詞字符,後跟空格字符,後跟單詞字符,後跟關閉單詞「)」或單詞字符後跟空格或結尾字符串

var str = "@Hello(first second), one two"; 
var res = str.match(/@\w+\(\w+\s\w+(?=\))\)|\w+(?=\s|$)/g); 
console.log(res); 
+0

謝謝。我的案例的解決方案是使用與RegExp/\ s(?!\ w + \))/ – Catarina