Simple regex: how can I say: “Good thing you have“ s ”or“ es ”or“ “at the end of a word, also match.”

I am using jQuery (I don't know if this is relevant), here is regex:

var re = new RegExp('\\b' + a_filter + '\\b'); 

Thus, it matches whole words in the a_filter variable, which has a bunch of words. Now it will match the wrench, but not the wrenches. He will match "chair", but not "chairs", he will match "john", but not "john's". I tried, but I can’t understand.

Can someone please help me adjust my regex above to allow this at the end of a word?

s es 's is what I want to resolve at the end of a word match, so I don’t need to include all the possible variations of each word. I think that all the words end with someone actually typing, if you know more, it would be great to get help, THANKS!

EDIT: here is my jsfiddle, maybe I had a_filter mixed with filter_tags , I think I'm doing it back, pah. ???

http://jsfiddle.net/nicktheandroid/mMTsc/18/

+4
source share
3 answers

I have a group of your endings after a filter concatenation, at ? match 0 or 1 is required.

 var a_filter = "wrench"; var re = new RegExp('\\b' + a_filter + '(s|es|\'s)?\\b'); alert( re.test("wrench's") ); 

Example: http://jsfiddle.net/qctAG/ ( alert() warning, you will get 4 of them)

+3
source

You need something similar to this:

 var re = new RegExp('\\b' + a_filter + '(s|es|\'s)?\\b'); 

Of course, this will not correspond to all plural numbers (for example, bulls, geese), and it will correspond to words that do not exist (for example, sheep).

+2
source

This works for me ... if you have an array!

 var a_filter = ["wrench","wrenches","wrench's"]; for(var i=0; i < a_filter.length; i++){ var re = new RegExp('\^' + a_filter[i] + '\$'); document.write(re.test("wrench's") + " " + a_filter[i] + "<br />"); } 

Here is the fiddle: http://jsfiddle.net/XCARd/2/ Play with re.test() to see what it matches.

+1
source

All Articles