I am trying to write a regex to extract the last few words from a sentence. However, I can't seem to make it work.
var str = "Tell me about robin hood";
const TELL_ME_ABOUT_REGEX = /^Tell me about (\w+\s*)*/
if( str.match(TELL_ME_ABOUT_REGEX) ){
var matches = TELL_ME_ABOUT_REGEX.exec( str );
console.log( "user wants to know about ",matches);
}
I am trying to get the word "robin hood". But I only end up with "hood".
[ 'Tell me about robin hood',
'hood',
index: 0,
input: 'Tell me about robin hood' ]
What do I change in my regex?
Why you need regex for this? You can do it without regex like it
var str = "Tell me about robin hood";
var str = str.split(" ").splice(3).join(" ");
alert(str);
http://codepen.io/anon/pen/aNvrYz
Edit : Regex Solution
var str = "Tell me about robin hood";
var match = str.match(/^Tell me about (.*)/i);
var textInDe = match[1];
alert(textInDe);
The problem with /^Tell me about (\\w+\\s*)*/
is that are multiples matches for "Tell me about robin hood"
, ie, robin
is a possible match, as well robin\\s
and so forth. The repetition characters may confuse you sometimes, but when you think on all match possibilities, it can be clearer. For matching all that came after Tell me about
you can simply get it all at once, with only one possible match: (.*)
.
Here is a correct regex:
^Tell me about ((?:\w+\s*)*)
Compare to your original one which is
^Tell me about (\w+\s*)*
That's so closed. The point is you should use the non-capturing group for an inner bracket and capturing group for an outer bracket.
Note that (\\w+\\s*)*
from your regex, it might captures robin
to \\1
at the first time and then overwrites hood
to \\1
. So, now you might understand that regex engine will work in this way.
Try this
Tell me about ([\w\s]+)
(\\w+\\s*)*
will capture 'robin' then 'hood' but only keep the last iteration => 'hood'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.