JavaScript operator similar to SQL “like” [duplicate] - javascript

This question already has answers here:
Emulating SQL LIKE in JavaScript
(10 answers)
Closed 8 years ago.
I have this function with jquery, but I need it to work like operator "like"
Help me.
Thank you very much for your help
works correctly if I look the whole word
example
var vsearch = "home"
var array = _.filter(objeto, function(product){
return product.filtro ==vsearch;
var vsearch=`H`
var array = _.filter(objeto, function(product){
return product.filtro ==vsearch;
No looks for containing the H
Thanks.

Use indexOf, which returns -1 if a string is not found, to see if the product contains the letter you're searching for.
var vsearch=`H`;
var array = _.filter(objeto, function(product){
return product.filtro.indexOf(vsearch) > -1;
}

You could try something like this:
var stringToSearch = "The full string to search goes here";
var searchTermContains = stringToSearch.search("H");
This will return a -1 if no match is found.

Related

String function in react [duplicate]

This question already has answers here:
regex for "mentions"
(2 answers)
Closed 1 year ago.
I want to get the word after the # from the string like. "hello coders am #ayub a beginner in react". want to get that (ayub) from the string after #. any help.
You can find the String.prototype.indexOf and String.prototype.substring
let data = "hello coders am #ayub a beginner in react";
let startIndex = data.indexOf('#');
let endIndex = data.indexOf(' ', (startIndex + 1));
let result = data.substring(startIndex+1, endIndex);
console.log(result);
Firstly, you do not need a React specific function to do that. You can write a simple javascript function like so -
function getName(str) {
return str.match(/#[a-z]+/)[0].substring(1);
}
console.log(getName("hello coders am #ayub a beginner in react"));
This is a pretty fast solution

Make .search() case insensitive for a variable [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 6 years ago.
I'd like to make .search() case insensitive for a variable I've entered into it. From the documentation on W3 it appears I can only hard code case insensitive searches. The example on W3 goes:
var str = "Mr. Blue has a blue house";
var n = str.search(/blue/i);
My code looks like:
var searchTerm = $("input").val();
var source = $("#page5").html();
var found = source.search(searchTerm);
If I were to code it like the W3 example then the .search() command would look like:
var found = source.search(/searchTerm/i);
However, when I do that it appears to attempt to search for the literal text of "searchTerm" instead of the value within the variable searchTerm. Is there a way to use the case insensitivity on the search method while inserting a variable into it?
untested:
var searchTerm = $("input").val().toLowerCase();
var source = $("#page5").html().toLowerCase();

javascript - check if string is in a array without case sensitive [duplicate]

This question already has answers here:
How to check if a string array contains one string in JavaScript? [duplicate]
(5 answers)
Closed 7 years ago.
I want to make a filter. Where if you input a word that is in the 'blacklist' it will tell something. I've got all the code but have a problem.
JS:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (input == array)
// I will do something.
} else {
// Something too
}
}
I want to make it so that if the input is a item in the array. That the statement is true. But what is the correct way to do this? Because what I'm doing here doesn't work! Also I want to get rid of the case sensitive! So that if the array has hello in it both hello and Hello are detected.
Sorry if this question is asked before. I searched for it but didn't know what keywords to use.
EDIT 1:
I am changing my question a little bit:
I want to check what is in my original question but with some other features.
I also want to check if input has a part of an item in array. So that if the input is hello that helloworld is being detected because is has hello in it. As well as hello or Hello.
Use indexOf:
if (array.indexOf(input) > -1)
It will be -1 if the element is not contained within the array.
This code should work:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) >= 0)
// I will do something.
} else {
// Something too
}
}
The indexOf Method is member of the array type and returns the index (beginning at 0) of the searched element or -1 if the element was not found.
I think what you are looking for is
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) !== -1 )
// I will do something.
} else {
// Something too
}
}

Check if a string contains a specific number in JavaScript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: string contains
I want to check if a string contains a specific number in JavaScript, how can I rewrite the statement s contain d in below code?
var s = '11/14/2012';
var d = '14';
if (s contain d) {
//...
}
My case is similar to Check if a string contains a certain number, but how can I implement this action in JavaScript?
Thanks
The easier way is to use index Of.
if(s.indexOf(d) > -1)
var s = '11/14/2012';
var d = '14';
if (s.match(d)) {
alert('Found');
}​

Replace function not replacing [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I followed some documentation to use the JavaScript replace function and it's not changing anything. No errors are thrown. Any idea what I'm doing wrong? The variable is retrieved from XML - maybe it needs to be cast as a string or something?
for (var i = 0, iln = projects.length; i < iln; i++){
var thumb = projects[i].get('thumb');
thumb.replace("200.jpg", "640.jpg");
console.log(thumb) //200.jpg not replaced
}
The full thumb value should look like this:
http://b.vimeocdn.com/ts/160/895/160895498_200.jpg
Is there a better way to find and replace things?
Assign the value back into thumb.
thumb = thumb.replace("200.jpg", "640.jpg");
Try:
thumb = thumb.replace("200.jpg", "640.jpg");

Categories

Resources