How Do I Perform A Javascript Match With A Pattern That Depends On A Variable?
The current implementation of Remy Sharp's jQuery tag suggestion plugin only checks for matches at the beginning of a tag. For example, typing 'Photoshop' will not return a tag nam
Solution 1:
If you want to do regex dynamically in JavaScript, you have to use the RegExp object. I believe your code would look like this (haven't tested, not sure, but right general idea):
for (i = 0; i < tagsToSearch.length; i++) {
var ctag = jQuery.trim(currentTag.tag);
var regex = newRegExp(ctag, "i")
if (tagsToSearch[i].match(regex)) {
matches.push(tagsToSearch[i]);
}
}
Solution 2:
Solution 3:
You could also continue to use indexOf, just change your === 0 to >= 0:
for (i = 0; i < tagsToSearch.length; i++) {
if (tagsToSearch[i].toLowerCase().indexOf(jQuery.trim(currentTag.tag.toLowerCase())) >= 0) {
matches.push(tagsToSearch[i]);
}
}
But then again, I may be wrong.
Post a Comment for "How Do I Perform A Javascript Match With A Pattern That Depends On A Variable?"