cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way to search for an exact match in a string?

soda
Contributor

Is there a way to search for an exact match in a string?

Hello,

I've got an array that has some items from a listview stored in it.  The entries look similar to this:



Smart Collection  2

Ratings    5

Colors    0

Collection  4

Publish Collection  5



I need to check each entry to verify that the correct number follows the entry.  In a data file I've got expected results.  Currently I am using indexOf to see if the expected item is in the array.  When I am searching for "Collection" it is finding the "Smart Collection" item.  Is there a way to perform an exact match when using indexOf?  Or, I have seen people suggest using the search method, however, I am not familiar with regular expressions which is what  the search method apparently needs?  I'll go look through some more examples of regex but have so far not found examples of what I am trying to do.  Does TC have anything like this builtin?  Thanks for any suggestions.

Mark
5 REPLIES 5
tinauser
Frequent Contributor

RE: Is there a way to search for an exact match in a string?

I´m also not a great expert of regexp, but this should do what you need

^Collection$
AlexeyK
Staff

RE: Is there a way to search for an exact match in a string?

Mark,


What script code do you use for the search? Could you post it here?

The solution depends on it. For instance, if the indexOf method is added to the JScript Array object via prototype, you may need to look at the prototype code. Perhaps, it does not use the === operator for comparison.

I’m not a member of the SmartBear Support Team, I’m just helping users in this community.
Any views or opinions expressed do not necessarily represent those of SmartBear Software.

-----
soda
Contributor

RE: Is there a way to search for an exact match in a string?

Thanks for the replies. I am going to try the ^Collection$ expression as well.



What I have had for code so far is this:

if((str.search(collectionSet) > -1) && str.search(collectionSet) < 1){

   - statements

}



and then decided I could just use:

if((str.search(collectionSet) == 0){

  -statements

}



But I am concerned about a case where for whatever reason there might be a space before the actual listview item name sotred in my array.  In which case, I guess I could strip leading spaces.



Any thoughts on that?
AlexeyK
Staff

RE: Is there a way to search for an exact match in a string?

Mark,


As far as I know, the search method finds a substring in a string using regular expressions.

It's possible to use regular expressions here, but I think that they work longer and require a bit more resources.

Why don't you use indexOf or ordinary comparison? E.g. --


    if(str.indexOf(collectionSet) == 0){

      ...


-- or --


    if(str == collectionSet){

      ...


The latter case implies that you have "full strings" specified in your baseline Excel sheet (that is, you have both "Smart Collection" and "Collection", not just "Collection").

I’m not a member of the SmartBear Support Team, I’m just helping users in this community.
Any views or opinions expressed do not necessarily represent those of SmartBear Software.

-----
soda
Contributor

RE: Is there a way to search for an exact match in a string?

Thanks Alex,

I started out using indexOf, and then thinking I was going to need to use regular expressions, I switched to .search.  I think you helped confirm that I can simply just go back to indexOf.  I thought I had a scenario that would require regex, but, this solution should work.  Thanks.
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors