2

I have a text area of words. There is a jQuery function that will loop over an array to search for each work within the text, if it find it puts it in an array, then displays it. This works well. The text area is editable allowing a user to modify the text and then search again (Allowing for correcting spelling or context). After modifying the text and doing the function again it only finds a substring of the string, not the full thing, see below:

Text to search: I live on the Q53 Route. I want to see the Q3 route near my house.

If you added Q44 at the end after the period it will only find Q4..

   $(document).ready(function() {
        findRoutes();
    });


function findRoutes(){
        $('#route_list').html('');

        var found_routes = [];


        var rts = ["Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12","Q13","Q15","Q15A","Q16","Q17","Q18","Q19","Q20A","Q20B","Q21","Q22","Q23","Q24","Q25","Q26","Q27","Q28","Q29","Q30","Q31","Q32","Q33","Q34","Q35","Q36","Q37","Q38","Q39","Q40","Q41","Q42","Q43","Q44-SBS","Q46","Q47","Q48","Q49","Q50","Q52-SBS","Q53-SBS","Q54","Q55","Q56","Q58","Q59","Q60","Q64","Q65","Q66","Q67","Q69","Q70-SBS","Q72","Q76","Q77","Q83","Q84","Q85","Q88","Q100","Q101","Q102","Q103","Q104","Q110","Q111","Q112","Q113","Q114","QM1","QM2","QM3","QM4","QM5","QM6","QM7","QM8","QM10","QM11","QM12","QM15","QM16","QM17","QM18","QM20","QM21","QM24","QM25","QM31","QM32","QM34","QM35","QM36","QM40","QM42","QM44","X63","X64","X68","QMT100","QMT101","QMT102","QMT103","QMT104","QMT105","QMT106","QMT107","QMT112","QMT115","QMT116","QMT117","QMT130","QMT131","QMT132","QMT133","QMT134","QMT135","QMT155","QMT156","QMT157","QMT160","QMT161","QMT162","QMT163","QMT164","QMT165","QMT166","QMT167","QMT168","QMT169","QMT170","M60","QT10","QT11","QT12","QT13","QT14","QT15","QT16","QT17","QT18","QT19","QT1","QT20","QT22","QT24","QT2","QT30","QT31","QT32","QT33","QT34","QT35","QT36","QT37","QT38","QT39","QT3","QT40","QT41","QT42","QT43","QT44","QT45","QT46","QT47","QT48","QT49","QT4","QT50","QT51","QT52","QT54","QT55","QT56","QT58","QT59","QT5","QT60","QT61","QT62","QT63","QT64","QT65","QT66","QT67","QT68","QT69","QT6","QT70","QT71","QT72","QT73","QT74","QT75","QT76","QT77","QT78","QT79","QT7","QT80","QT81","QT82","QT83","QT84","QT85","QT86","QT87","QT88"];

var comment = $("#comment_div").val();

        jQuery.each(rts, function(index, item) {
//          if (comment.indexOf(item) >= 0) {
            if (comment.includes( item )) {
                found_routes.push(item);
            }
        });

 jQuery.each(found_routes, function(index, item) {
            $('#route_list').append(item+"<br>"); 

        });
        var final_routes = found_routes.toString();
            $("#string_routes").val(final_routes);
}

See this fiddle for the full example. Thanks for the help.

2 Answers 2

2

For this to work as you expect you need to ensure a full match on the Qxx value, not a partial match. To do that you can use a regular expression utilising word boundaries:

let comment = 'I live on the Q53 Route. I want to see the Q3 route near my house. Q44'
let  rts = ["Q1", "Q12", "Q3", "Q33", "Q4", "Q44"];
let found_routes = [];

jQuery.each(rts, function(index, item) {
  var re = new RegExp('\\b' + item + '\\b', 'gi');  
  if (re.test(comment)) {
    found_routes.push(item);
  }
});

console.log(found_routes);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Alternatively, if all the Qxx values follow the same format of a Q followed by 1 or 2 digits, then you can avoid the loop and use a regex to capture all of them:

let comment = 'I live on the Q53 Route. I want to see the Q3 route near my house. Q44'
var re = new RegExp(`\\b(Q\\d{1,2})\\b`, 'gi');
var matches = comment.match(re);
console.log(matches);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, your first first idea seems to work great! Thanks so much. I am not familiar enough with Regex. Unfortunately there are other formats so the second idea won't work but good to keep in mind.
No problem, glad to help. The second method is by far the most extensible, and can easily be amended if you can formulate the pattern.
0

Looks like you do not have Q44 in your list and that's why it does not find it. But on the other hand, you have Q4 so in your list and it finds is.

Maybe before searching you should replace all commas and dots with spaces and then add space before and after each substring you are searching for? In such case you will avoid such false matches.

1 Comment

Thanks Nick, bad example on my part. The space idea is a good thought, would have been a good hack.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.