Get only the last match? .Match (word)

I have a regex to get @user from a text box. When the user enters something using @, I get it.

My problem is that I want to get only the last match, not all of them.

eg:

user type:

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

my code is as follows:

 @josh,@marie,@anne 

Can I get only the latest @something record? (while the user is typing)

 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/ (click on textarea)

+6
source share
5 answers

Besides getting all matches and getting the last, you can use capture groups to get the last match:

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

Or using exec, everything will look like this:

 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, if your goal is a more general approach, and you need to switch between getting all the words and one, I would recommend keeping the global correspondence and getting the last, as in Jonas answer.

+4
source

My suggestion is that you only show the last record of your results.

You can do this by changing the line: var name = content.match(word);

to

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

In more detail what this does, it gets all the results from your regular expression, than turns it into an array, breaking each word. Then it assigns the last element of the array to the name variable.

Hope this was helpful.

+4
source

You can simply select or swap the last match in the match array returned by .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

+2
source

Use this regular expression '/ @ (\ w +) $ / ig' insted from '/ @ (\ w +) / ig'.

And then your code will work like a charm .;)

 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(); }); 

See how he hears https://jsfiddle.net/dcs5pat8/1/

+1
source

I like the answer when you take your list with all the names @, @ name1, @ name2 and just turn off the last one, but here it is in just one step

 //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/

+1
source

All Articles