2

I need to find a regular expression that would be able to work around an issue I am having.

Query: barfly london

Should match: Camden Barfly, 49 Chalk Farm Road, London, NW1 8AN

I've tried many, many regex's for this, but none have worked so far. I am considering that maybe I will need to split the search into two separate queries for it to work.

Could anyone point me in the right direction? I'm a bit new to this area.

3
  • 1
    Would a non-regex answer suffice? Commented Jul 29, 2009 at 12:51
  • Is the order of the words in the query to be preserved in the subject string? Commented Jul 29, 2009 at 13:35
  • no the order of words does not matter, as long as they are all present Commented Jul 29, 2009 at 14:20

5 Answers 5

3

Try this:

var r = /barfly|london/gi
str = "Camden Barfly, 49 Chalk Farm Road, London, NW1 8AN"
alert(str.match(r).length>1)
Sign up to request clarification or add additional context in comments.

2 Comments

this is pretty darn close to what I need, the only problem is it must match all not any
I stand by my solution because the final test of the length is to ensure there are at least two matches. It would only fail in edge cases, like living on Barflybarfly road in Manchester.
2

I'd suggest not using regexs if you want to search for two string literals, and instead use normal string searching twice:

var test="Camden Barfly, 49 Chalk Farm Road, London, NW1 8AN"
if ((test.indexOf("Barfly") != -1) && (test.indexOf("London") != -1)) {
   alert("Matched!");
}

If you're not concerned about case-sensitivity, then you can just lowercase/uppercase your test string and your string literals accordingly.

Comments

1

Check this:

var my_text = "Camden Barfly, 49 Chalk Farm Road, London, NW1 8AN, london again for testing"
var search_words = "barfly london";

String.prototype.highlight = function(words_str)
{
    var words = words_str.split(" ");
    var indicies = [];
    var last_index = -1;
    for(var i=0; i<words.length; i++){
        last_index = this.toLowerCase().indexOf(words[i], last_index);
        while(last_index != -1){
            indicies.push([last_index, words[i].length]);
            last_index = this.toLowerCase().indexOf(words[i], last_index+1);
        }

    }
    var hstr = "";
    hstr += this.substr(0, indicies[0][0]);
    for(var i=0; i<indicies.length; i++){
        hstr += "<b>"+this.substr(indicies[i][0], indicies[i][1])+"</b>";
        if(i < indicies.length-1) {
            hstr += this.substring(indicies[i][0] + indicies[i][1], indicies[i+1][0]);
        }
    }
    hstr += this.substr(indicies[indicies.length-1][0]+indicies[indicies.length-1][1], this.length);
    return hstr;
}

alert(my_text.highlight(search_words));
// outputs: Camden <b>Barfly</b>, 49 Chalk Farm Road, <b>London</b>, NW1 8AN, <b>london</b> again for testing 

Comments

0

theString.match( new RegExp( query.replace( ' ', '\b.*\b' ), 'i' ) )

Comments

0

Dominic's solution without case sensitivity. This is what I needed for my project.

var test="Camden Barfly, 49 Chalk Farm Road, London, NW1 8AN";
if ((test.toLowerCase().indexOf("barfly") != -1) && (test.toLowerCase().indexOf("london") != -1)) {
    alert("Matched");
}
else {
    alert("Not matched");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.