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

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();

Related

Javascript array variable when named "name" showing unexpected result when storing strings [duplicate]

This question already has answers here:
Why does JavaScript split() produce different output with different variable names?
(3 answers)
var name produces strange result in Javascript [duplicate]
(5 answers)
Closed 3 years ago.
I am writing a very simple code for storing strings in an array and then splitting it but apparently the "variable name" seems to have an impact on the results.
I have tried this on Google Chrome console as well as Microsoft edge. Results are the same.
var fullName = "Jonathan Archer";
var name = fullName.split(" ");
console.log(name[0]);
//The output of the above code is : "J"
var userName = fullName.split(" ");
console.log(userName[0]);
//The output of the above code is: "Jonathan"
//Also tried following, also exhibited same behavior as above
var name = ["Jonathan", "Archer"];
var userName = ["Jonathan", "Archer"];
console.log(name[0]);
console.log(userName[0]);
I don't see why these two code snippets are producing different results. Is there any restriction for using "name" as an array name in JavaScript?
Do not use name as variable name, Because it will conflict with window.name
var fullName = "Jonathan Archer";
var n = fullName.split(" ");
console.log(n[0]);
//The output of the above code is : "J"
var userName = fullName.split(" ");
console.log(userName[0]);
//The output of the above code is: "Jonathan"
//Also tried following, also exhibited same behavior as above
var n = ["Jonathan", "Archer"];
var userName = ["Jonathan", "Archer"];
console.log(n[0]);
console.log(userName[0]);

Issue getting my objects properties [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 4 years ago.
Hi guys I have just started learning to code and I have hit a road block in accessing the properties of the object I have created.
Here is my object.
var restaurantOrder = {
"my entree": "cheeseburger",
"my side": "fries",
"the drink": "water"
};
I would like to get the value of entree however nothing I am trying seems to work :(
Here is what I have tried.
var entreeValue = restaurantOrder.my entree;
var entreeValue = restaurantOrder[my entree];
var entreeValue = restaurantOrder.[my entree];
var entreeValue = restaurantOrder.["my entree"];
None of the above lines work :( Thank you for your help.
Because the properties of your restaurantOrder object have spaces, you will need to use [] to access them. You cannot use . like you can with a property name that is a single word.
Also, because of the spaces you will need to enclose the property name with quotes, so either:
var entreeValue = restaurantOrder["my entree"];
or
var entreeValue = restaurantOrder['my entree'];
will work.

JavaScript operator similar to SQL “like” [duplicate]

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.

Simple Javascript Replace not working [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 3 years ago.
This seems so simple and trivial but it is not working. Here is my javascript:
var url = "/computers/";
console.log(url);
url.replace(/\//gi, " ");
console.log(url);
And here is the output in my browsers console:
/computers/
/computers/
As you can see nothing changes. As you can tell from the code I'm trying to replace the forward slashes with spaces. What am I doing wrong?
url = url.replace(/\//gi, " ");
Nothing changes because you're not assigning the result of the replacement to a variable. Add url = url.replace()
url.replace(/\//gi, " "); returns the resulting string (in javascript you can't modify an existing string), you are not assigning it to anything
assign it like so:
url = url.replace(/\//gi, " ");

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