2016-03-03 37 views
6

我有一個正則表達式來從一個textarea獲取@user。當用戶用@鍵入某個東西時,我可以得到它。只得到最後一場比賽嗎? .match(word)

我的問題是,我想得到最後一場比賽,而不是全部。

如:

用戶類型:

@josh and @marie = want to show @marie 
@josh loves @marie and @anne = show @anne 

我的代碼表明這樣的:

@josh,@marie,@anne 

我能得到公正的最後@something進入? (當用戶輸入時)

var word=/@(\w+)/ig; 

$("#comment").on("keyup",function() { 

    var content = $(this).val(); 
    var name = content.match(word); 
    var dataString = name; 

    if(name.length > 0) { 
     $("#result").text(name); 
    } 
    return false(); 
}); 

HTML

<textarea id=comment>@josh and @marie</textarea> 
<div id=result></div> 

https://jsfiddle.net/dcs5pat8/(上textarea的按)

+1

的可能的複製[JavaScript的正則表達式:我能獲得最後的匹配指數或向後搜索/ RightToLeft?](http://stackoverflow.com/questions/4313918/javascript-regexp-can-i-get-the-last-matched-index-or-search-backwards-righttol) – JJJ

+1

看看這個 blog.bigbinary.com/2010/03/31/regular-expressions-in-JavaScript.html 匹配方法 匹配方法的作用與exec方法完全相同,如果沒有傳遞g參數。當全局標誌打開時,匹配返回一個包含所有匹配的數組。 – mfadel

+0

使用子串怎麼樣? – mfadel

回答

4

除了獲取所有的比賽和獲得最後一個,你可以使用捕捉組來獲得最後一場比賽:

var word=/.*(@\w+)/i; 
var name = content.match(word)[1]; 

或者使用EXEC,整個看起來像:

var word=/.*(@\w+)/i; 

$("#comment").on("input",function() { //changed keyup to input 

var content=$(this).val(); 
var match = word.exec(content); 

if(match){ 
$("#result").text(match[1]); 
} 

}); 

Fiddle

PS,如果你的目標是一個更通用的方法,你需要讓所有的單詞和一個一個之間進行切換,我建議你保持全球比賽並獲得最後的作爲喬納斯的回答。

+0

你怎麼能看到小提琴的結果? –

+1

@MrRubix小提琴使用與原始文章相同的方法,並對文本框中的鍵入事件作出反應。即使在文本框中使用方向鍵也會觸發事件(@ OP,而不是你可以在'input'或'change'上使用的keyup) –

+0

我會盡力的!是否很難獲得最後一場比賽?例如用戶類型'@ a'' @ c'並且希望在中間添加'@ b',所以我的數組輸出將是'@ a'' @ c'' @ b'因爲b是最後一個他類型。 –

1

使用這個表達式'/ @(\ w +)$/IG' insted的'/ @(\ w +)/ ig'。

然後你的代碼將像魅力一樣運行。 ;)

var word=/@(\w+)$/ig; 

$("#comment").on("keyup",function() { 

var content=$(this).val(); 
var name = content.match(word); 
var dataString = name; 

if(name.length>0){ 
$("#result").text(name); 
} 
return false(); 
}); 

看到它聽到https://jsfiddle.net/dcs5pat8/1/

+2

如果'@ word'不是這個短語的最後一個單詞,也就是說,如果你有@john愛@marie土豆,你的正則表達式什麼也找不到。 –

4

我的建議是,你只顯示你的結果的最後一項。

你可以通過改變線路: var name = content.match(word);

var names = content.match(word).split(','); 
var name = names[names.length - 1]; 

更多細節,這樣做是它會從你的正則表達式所有的結果,比它把它變成數組,分裂每個詞。然後它將數組的最後一項歸入name變量。

希望這有幫助。

+0

我試過了,但它似乎不工作... https://jsfiddle.net/dcs5pat8/7/ –

+0

'content.match(word)'已經返回一個數組,沒有理由/需要分割它。 – JJJ

+1

您不需要在比賽結束時使用「split」功能。我正在編輯它,但有一個待處理的編輯。請看這個小提琴:https://jsfiddle.net/dcs5pat8/9/ –

1

我不喜歡,你把你的列表中所有的@名稱的答案,@名1,@ NAME2和公正的分裂是最後一個,但在這裏它是隻需一個步驟

//split on @something 
//the penultimate item is our target 
//if there is < 2 items there weren't any @somethings so return '' 
user = (split = "testing @charlie testing".split(/(@[^ ]*)/)).length > 1 ? split.splice(-2,1)[0] : ''; 

https://jsfiddle.net/ek19h0fb/1/

2

你可以簡單地選擇或彈出()匹配的陣列通過.match返回在最後一場比賽中

var word=/@(\w+)/ig; 

$("#comment").on("keyup",function() { 

    var content=$(this).val(); 
    var matches = content.match(word); 
    var lastmatch = matches.pop(); 

    //IF YOU NEED TO KEEP INTACT THE VAR MATCHES 
    //var lastmatch = matches[matches.length - 1]; 


    if(name.length>0){ 
     $("#result").text(lastmatch); 
    } 
    return false(); 
}); 

JSFiddle