if(kword == term){
$(this).trigger('click');
}
The case is if the kword is "car" and the term is "cars", I would want that to be a positive match.
Currently I'm looking at an exact match. As I'm a novice at jquery I don't know how to do this. Can anyone point me to the right direction?
You can use indexOf() to find the string inside another
if(term.indexOf(kword)>-1){
//code
}
If you need to simply compare a string variable with a different string variable with an 's' in the end, you can go with following code:
if (term === kword + 's') {
...
}
If you need to check specifically if terms is a plural version of kword, you would need much larger implementation, featuring pluralize function (that you need to implement or search for a library):
if (term === pluralize(kword)) {
...
}
function pluralize(word) {
//implement function
}
If that doesn't not answer your question, please be more clear about what you need to do.
As #Anton said, you can use indexOf(), it returns the position of the first occurrence of a specified value in a string otherwise returns -1 if the value to search for never occurs. Also note that this method is case sensitive.
Related
Here is a part of ag-grid JS code that filter cells with exactly text:
function doesExternalFilterPass(node) {
switch (status) {
case 'released':
return node.data.Status == 'Released';
case 'early-access':
return node.data.Status == 'Early access';
case 'in-development':
return node.data.Status == 'In development';
case 'rtt-strategy':
return node.data.Genre == /.'RTT'./;
default:
return true;
}
}
For example, one of the strings filter all cells containing Released in Status column, other containing RTT in Genre column. But I need that strings like RTT, RTS, Action also to be filtered. As I understand, I should use regular expressions, I try this
return node.data.Genre == /.'RTT'./;
but it doesn't work.
Any ideas to get many-words filtering?
You don't need regex for this. Regex should be avoided, if possible. If I understand it correctly, you want to check if a string contains a substring. For that you can use String.prototype.includes() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
Example code:
case 'rtt-strategy':
return node.data.Genre.includes('RTT');
If you still want to use regex, then you can use the RegExp.prototype.test() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test. First of all, you really should look up how to use regex in js, because your example code is way off. Secondly, example code:
/RTT/.test(node.data.Genre)
Or if you want case insensitive, then:
/RTT/i.test(node.data.Genre)
How can I make a function that takes a string and checks if it is a number string or if it includes letters/other characters? I have no idea what to do... RegExp takes too long so is there another way?
You have to use isNaN() function (is Not a Number). It will return you true if it's not a number (that mean that it contains letter) and false if it's one.
Source :
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN
You can check for isNaN and see if value is number if you don't want to go with RegExp.
let inpArgs = Number.parseInt(input);
if(!Number.isNaN(inpArgs)){
// this check ensures that your input is number
// Do what you have to do
}
else{
// Handle the error
}
But I would prefer the one line check using RegExp any day like below.
if(/^\d+$/.test(Number(input))){
// this says your input is Number
}
You can use typeof oprator to check whether it is a number or string.
function(anything){
if(typeof(anything)==='number'){ //do something} }
if(typeof(anything)==='string'){ //do something} }
Hope I answer your question.
Thanks.
You can use typeof in JavaScript to identify input Like
alert(typeof <your input>); or var identity = typeof <your input>;
and whatever string alert that match in Condition Like
if (identity == <alert message>){do something}else{do something}
I want to trigger a digital marketing tag on every page which falls under a particular URL path, say example.com/sachin under the sachin directory.
I've tried if (location.href === 'example.com/sachin.*/') but somehow the condition doesn't seem to work.
What will be the correct if condition for location.href if I want to cover all different resources with in the URL path say under sachin directory?
I presume you want to check if the URL contains example.com/sachin. It's highly rarely that any URL ever would contain 4 forward-slashes but what you would do is utilize indexOf.
if(location.href.indexOf("example.com/sachin/") != -1){
//Do something
}
This basically says, if "example.com/sachin/" is found somewhere in the given string(in location.href in this case) on an indexposition that is not -1(which means that it doesn't exist), then execute.
You need to use Regular Expressions to match needed resources.
Something like that:
if(location.href.match(/^http:\/\/example.com\/sachin\//)){
//your staff here
}
Another approach to check for a specific directory in a url.
function urlContains(url, value) {
return ~url.indexOf(value);
}
if (urlContains(location.href, "/sachin/")) {
// found
} else {
// not found
}
The indexOf method checks a string for the value that is passed and returns -1 if a result was not found.
Instead of checking for == -1 or != -1 you can use the Bitwise NOT operator ~ to convert -1 to 0, which is a falsy value, non-zero values are treated are truthy values in javascript.
I have a variable [ITEM_NAME] that stores an item name from a shopping cart. I want to check whether the value in [ITEM_NAME] is like one of the following strings:
"CHICAGO NEW"
"CHICAGO OLD"
"CHICAGO TEXT"
"CHICAGO PURE"
In pseudocode:
if ([ITEM_NAME].contains("CHICAGO"))
I want a condition that will be satisfied for all strings that start with "CHICAGO".
This has nothing to do with jQuery selectors. Simply use the indexOf method to find whether the string stored in your variable contains the substring "CHICAGO":
if (yourString.indexOf("CHICAGO") !== -1) {
//do something
}
If you want to check whether the string starts with the text "CHICAGO", you can use:
if (yourString.indexOf("CHICAGO") == 0) {
//do something
}
You can read more about indexOf here.
EDIT: In your comments to Derek's answer, you mentioned that matching should be case insensitive (although this information isn't present in the question). If that is the case, simply use:
if (yourString.toUpperCase().indexOf("CHICAGO") == 0) {
//do something
}
.contains() checks to see if an element is a direct descendent of another DOM element. It is not useful at all for what you are trying to do.
You should use .indexOf().
If you need your test to be case-sensitive:
if ([ITEM_NAME].indexOf('Chicago') === 0)
Otherwise, if it needs to be case-insensitive:
if ([ITEM_NAME].toUpperCase().indexOf('CHICAGO') === 0)
I would use a regex
if (string.match(/chicago/i)) {...}
it's case insensitive
I'm trying to search for all elements in a web page with a certain regex pattern.
I'm failing to understand how to utilize Javascript's regex object for this task. My plan was to collect all elements with a jQuery selector
$('div[id*="Prefix_"]');
Then further match the element ID in the collection with this
var pattern = /Prefix_/ + [0 - 9]+ + /_Suffix$/;
//Then somehow match it.
//If successful, modify the element in some way, then move onto next element.
An example ID would be "Prefix_25412_Suffix". Only the 5 digit number changes.
This looks terrible and probably doesn't work:
1) I'm not sure if I can store all of what jQuery's returned into a collection and then iterate through it. Is this possible?? If I could I could proceed with step two. But then...
2) What function would I be using for step 2? The regex examples all use String.match method. I don't believe something like element.id.match(); is valid?
Is there an elegant way to run through the elements identified with a specific regex and work with them?
Something in the vein of C#
foreach (element e in
ElementsCollectedFromIDRegexMatch) { //do stuff }
Just use the "filter" function:
$('div[id*=Prefix_]').filter(function() {
return /^Prefix_\d+_Suffix$/.test(this.id);
}).each(function() {
// whatever you need to do here
// "this" will refer to each element to be processed
});
Using what jQuery returns as a collection and iterating through it is, in fact, the fundamental point of the whole library, so yes you can do that.
edit — a comment makes me realize that the initial selector with the "id" test is probably not useful; you could just operate on all the <div> elements on the page to start with, and let your own filtering pluck out the ones you really want.
You can use filter function. i.e:
$('div[id*="Prefix_"]').filter(function(){
return this.id.match(/Prefix_\d+_Suffix/);
});
You could do something like
$('div[id*="Prefix_"]').each(function(){
if($(this).attr('id').search(/do your regex here/) != -1) {
//change the dom element here
}
});
You could try using the filter method, to do something like this...
var pattern = /Prefix_/ + [0 - 9]+ + /_Suffix$/;
$('div[id*="Prefix_"]').filter(function(index)
{
return $(this).attr("id").search(pattern) != -1;
}
);
... and return a jQuery collection that contains all (if any) of the elements which match your spec.
Can't be sure of the exact syntax, off the top of my head, but this should at least point you in the right direction