var test = "Hello all, this is a message and i want to mention @john, @smith and @jane...";
,我希望得到的是:拆分多個單詞開始用@符號
var result = ["john", "smith", "jane"];
我可以把最後一個用戶名字符串,但不是所有的人英寸對於正則表達式或其他字符串函數,我沒有問題。
謝謝。
var test = "Hello all, this is a message and i want to mention @john, @smith and @jane...";
,我希望得到的是:拆分多個單詞開始用@符號
var result = ["john", "smith", "jane"];
我可以把最後一個用戶名字符串,但不是所有的人英寸對於正則表達式或其他字符串函數,我沒有問題。
謝謝。
看來,它使用一個單一的正則表達式是不可能的:
var result = test.match(/@\w+/g).join('').match(/\w+/g);
您可能需要處理的情況,其中,正則表達式找不到:
var result = test.match(/@\w+/g);
result = result ? result.join('').match(/\w+/g) : [];
試試這個正則表達式
/(^|\W)@\w+/g
的JavaScript:
var test = "Hello all, this is a message and i want to mention @john, @smith and @jane";
var names = test.match(/(^|\W)@\w+/g);
console.log(names);
結果:
0: "@john"
1: "@smith"
2: "@jane"
活生生的例子在RegExr: http://regexr.com?36t6g
@Joren id idnt實際上嘗試了很多。我看到另一個問題,只能從字符串中獲得一個值。 – alix