admin管理员组文章数量:1327990
I have this code:
var search_term = postAdminID
// Wildcards Search
var search = new RegExp(search_term, "i");
sdsFilter = $.grep(sdsInfo.products, function(element, index) {
var sted = search.exec(element.AdminID)
return sted;
postAdminID is the index of a spesific post in an array.
element.AdminID is the corresponding index I am seraching for.
PROBLEM: Whenever I try to execute this search I get every matching index also parts of indexes matched.
Lets say I want to retrieve index #78. The above code returns: 78, 178, 278, 10078 and so forth. I only want the exact index - not every index containing the index.
I have tried .exec and test I have looked in every post here on stackoverflow that I can find.
Please help. How can I specify for regex that it should only look for the entire "string" ?
I have this code:
var search_term = postAdminID
// Wildcards Search
var search = new RegExp(search_term, "i");
sdsFilter = $.grep(sdsInfo.products, function(element, index) {
var sted = search.exec(element.AdminID)
return sted;
postAdminID is the index of a spesific post in an array.
element.AdminID is the corresponding index I am seraching for.
PROBLEM: Whenever I try to execute this search I get every matching index also parts of indexes matched.
Lets say I want to retrieve index #78. The above code returns: 78, 178, 278, 10078 and so forth. I only want the exact index - not every index containing the index.
I have tried .exec and test I have looked in every post here on stackoverflow that I can find.
Please help. How can I specify for regex that it should only look for the entire "string" ?
Share Improve this question asked May 30, 2017 at 15:06 XanderManXanderMan 1431 silver badge15 bronze badges 3- 1 What does the string you are trying to match look like? – atomrc Commented May 30, 2017 at 15:10
- I'm assuming you're trying to find one in a list? You would need to add whatever you're using to delimit the items to the search pattern, otherwise it's executing globally because it's treating it as a search souce: MDN – Patrick Barr Commented May 30, 2017 at 15:11
- What does search_term include? Edit your question and include necessary details. A bit of code does no good without the regex pattern and sample data. – C Perkins Commented May 30, 2017 at 20:00
3 Answers
Reset to default 5The issue is the search pattern you are using.
In REGEX you can match the beginning and the end of the line (of the string in the case of a single-line string). For this, add ^
at the beginning of the search pattern, and $
at the end.
For example ^78$
will only match the index 78, not 178,278...
If you're looking in a string with delimiters that contains many indexes, you will have to add the delimited in the search pattern, or split the string and search its elementary element.
Try this regex :
\b78\b
\b
will make sure this is only 78, and not a number containing 78.
In your case, search_term should be :
var search_term = "\b"+postAdminID+"\b"
Thank you for your input. Both suggestions did make things work. Except that they restricted use of the script for other purposes. I wanted to use one search algorithm for more than just integer keys.
In the end I ended up with this script, using: return element.AdminID == postAdminID;
var search_term = postAdminID
// Wildcards Search
var search = new RegExp(search_term, "i");
sdsFilter = $.grep(sdsInfo.products, function(element, index) {
var sted = search.exec(element.AdminID)
return element.AdminID == postAdminID;
});
I also added several search.exec to the mix in order for my script to bee more versatile and flexible. But thank you again for your valuble input.
本文标签: javascriptregex test How to return only exact matchStack Overflow
版权声明:本文标题:javascript - regex test How to return only exact match - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742247594a2440143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论