Wildcard string comparison in Javascript
Wildcard string comparison in Javascript
let's say I have an array with many Strings Called "birdBlue", "birdRed" and some other animals like "pig1", "pig2).
Now I run a for loop that goes through the array and should return all birds. What comparison would make sense here?
Animals == "bird*"
was my first idea but doesnt work. Is there a way to use the operator * (or is there something similar to use?
Answer by lenden for Wildcard string comparison in Javascript
if(mas[i].indexOf("bird") == 0) //there is bird
You.can read about indexOf here: http://www.w3schools.com/jsref/jsref_indexof.asp
Answer by Davsket for Wildcard string comparison in Javascript
You should use RegExp (they are awesome) an easy solution is:
if( /^bird/.test(animals[i]) ){ // a bird :D }
Answer by Cody Reichert for Wildcard string comparison in Javascript
You could use Javascript's substring method. For example:
var list = ["bird1", "bird2", "pig1"] for (var i = 0; i < list.length; i++) { if (list[i].substring(0,4) == "bird") { console.log(list[i]); } }
Which outputs:
bird1 bird2
Basically, you're checking each item in the array to see if the first four letters are 'bird'. This does assume that 'bird' will always be at the front of the string.
So let's say your getting a pathname from a URL :
Let's say your at bird1?=letsfly - you could use this code to check the URL:
var listOfUrls = [ "bird1?=letsfly", "bird", "pigs?=dontfly", ] for (var i = 0; i < list.length; i++) { if (listOfUrls[i].substring(0,4) === 'bird') { // do something } }
The above would match the first to URL's, but not the third (not the pig). You could easily swap out url.substring(0,4)
with a regex, or even another javascript method like .contains()
Using the .contains()
method might be a little more secure. You won't need to know which part of the URL 'bird' is at. For instance:
var url = 'www.example.com/bird?=fly' if (url.contains('bird')) { // this is true // do something }
Answer by James Hay for Wildcard string comparison in Javascript
var searchArray = function(arr, str){ // If there are no items in the array, return an empty array if(typeof arr === 'undefined' || arr.length === 0) return []; // If the string is empty return all items in the array if(typeof str === 'undefined' || str.length === 0) return arr; // Create a new array to hold the results. var res = []; // Check where the start (*) is in the string var starIndex = str.indexOf('*'); // If the star is the first character... if(starIndex === 0) { // Get the string without the star. str = str.substr(1); for(var i = 0; i < arr.length; i++) { // Check if each item contains an indexOf function, if it doesn't it's not a (standard) string. // It doesn't necessarily mean it IS a
There is an long list of caveats to a method like this, and a long list of 'what ifs' that are not taken into account, some of which are mentioned in other answers. But for a simple use of star syntax this may be a good starting point.
Answer by Spen for Wildcard string comparison in Javascript
I think you meant something like "*" (star) as a wildcard for example:
- "a*b" => everything that starts with "a" and ends with "b"
- "a*" => everything that starts with "a"
- "*b" => everything that ends with "b"
- "*a*" => everything that has a "a" in it
- "*a*b*"=> everything that has a "a" in it, followed by anything, followed by a "b", followed by anything
or in your example: "bird*" => everything that starts with bird
I had a similar problem and wrote a function with RegExp:
alert(matchRuleShort("bird123", "bird*")); //short code function matchRuleShort(str, rule) { return new RegExp("^" + rule.replace("*", ".*") + "$").test(str); } //Explanation code function matchRuleExpl(str, rule) { // "." => Find a single character, except newline or line terminator // ".*" => Matches any string that contains zero or more characters rule = rule.replace("*", ".*"); // "^" => Matches any string with the following at the beginning of it // "$" => Matches any string with that in front at the end of it rule = "^" + rule + "$" //Create a regular expression object for matching string var regex = new RegExp(rule); //Returns true if it finds a match, otherwise it returns false return regex.test(str); }
If you want to read more about the used functions:
- RegExp "^, ., *, $"
- string.replace(searchvalue,newvalue)
- new RegExp(pattern[, flags])
- RegExpObject.test(string)
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment