0

I am building a simple search where query string can have wild cards '*'. Search terms can be like following:

  • animal
  • ani*
  • *mal
  • an*al

all above should return true if the word is 'animal'.

how this can be done in JS / jquery?

will appreciate for help. rnv

5
  • 3
    Replace * by .* before applying regex Commented Jul 31, 2015 at 8:44
  • use regular expressions for that Commented Jul 31, 2015 at 8:45
  • I guess you are looking for \w*, else .* will match even any animal with an*al, and you say you need to match 1 word. The problem here is with Unicode letters. Do you need to also support Unicode? Commented Jul 31, 2015 at 8:49
  • You're asking about regular expressions, but I think the heart of your question is really about Approximate string matching. Is that true? Commented Jul 31, 2015 at 8:56
  • Yes. but can you give me hint how to use approximate string matching using jquery or js? Commented Jul 31, 2015 at 11:44

5 Answers 5

2

The match on a string is simple:

var mystring = "This is my string";
var myregex = /*some regex*/
var results = mystring.match(myregex); // you can also write the regex directly as the argument of the match method, without using any variable.

So in your case you could have:

var mystring = "animal";
var myregex = new RegExp(inputQuery.replace(/\*/g, '.*'), 'gi'); // gi stands for 'global' and 'ignorecase' when applying the regex
var results = mystring.match(myregex);

Beware that .* matches zero or more (comes from the * whildcard) character, ANY character (comes from the .)
If you want to match zero or more letter, number or underscoreuse \w*, if you want to match one or more, use \w+, and if you want to match a specific number of letters, use \w{x} (\w{3} matches exactly 3 letters).

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

3 Comments

replace('', '.') replaces only one instance of ''. is there a way to replace all instances? query can be 'aim*l'
@user1749707 string.replace(searchvalue,newvalue) actually does replace ALL instances of the searchvalue with newvalue. Here's the docs.
@user1749707 I updated the answer. I forgot to mention that you should use a regex with the global flag set, in order to replace ALL occurrences of a string, sorry :)
1
var str = "anim*";
var replaced = str.replace("*", ".*");
var regex = new RegExp(replaced);
var result = regex.test("animal");
console.log(result);

change the str variable to get the result as true or false;

1 Comment

Thanks works fine but if the query string contains multiple '' then it failes. e.g. str='anm*l'.
0

Implemented version - https://jsfiddle.net/dpoqnacv/1/

var regexString = '^'+ $('#searchbox').val().replace("*",".*") + '$';

    if(new RegExp(regexString).test('animal'))
          $('#resultdiv').html('Matching...');
    else
        $('#resultdiv').html('Not Matching...');

1 Comment

you may need to do a global replace of * if you are going to have multiple wildcards in the search.
0

You can just transform your wildcard into a RegExp and perform your search. Here is a simple example.

var search = document.getElementById("search");
var result = document.getElementById("result");


result.style.color = "red";

function fsearch() {
    var str=search.value;
    str = str.replace("*", ".*") //Transform your wildcard into a RegExp
    result.innerHTML = "animal".match(new RegExp(str));
}
<label for="search">Search : <input name="search" id="search" type="text"/></label>
<input id="ok" type="button" value="ok" onclick="fsearch()"/>
Result : <div id="result"></div>

Comments

0

not bad answers but i think its easier with .test();

var str = "my dog is an animal";

/dog*anim*/.test(str); //returns true
/d*mal/.test(str); //returns true

etc

give it a try

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.