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

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');
}​

Related

Writing letter directly behind a value [duplicate]

This question already has answers here:
How do I concatenate a string with a variable?
(5 answers)
Closed 3 years ago.
I need to assign a time to a profile value from our database, but I don't know how to seperate the values.
It is the following code:
var pauze_datum = new Date("2020-01-27T10:33:00");
The time should be assigned to var Temporary_Date which is a filled in date in a form. How do you seperate the letter e and T?
var pauze_datum = new Date("Temporary_Date T10:33:00");
You can use either regular concatenation:
var value = "2020-01-27";
var pauze_datum = new Date(value + "T10:33:00");
or template literals:
var value = "2020-01-27";
var pauze_datum = `${value}T10:33:00`;

Add double quotes to arraylist of string in javascript [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 3 years ago.
I am new to stack overflow learning javascript and programming.
I have a problem that i am stuck while learning and thinking any help on this question will be
useful for me thanks and the question is:
Example i have a variable a in the code below and i want to convert it to an array in javascript
var a = ["baby,cat,dog"]
i wanted it to be
a = ["baby","cat","dog"].
Use String.prototype.split() as below
var a = ["baby,cat,dog"];
a = a[0].split(',');
console.log(a);
You could take the array and map the splitted values and get a flat array back.
var array = ["baby,cat,dog"];
result = array.flatMap(s => s.split(','));
console.log(result);

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.

Create multidimensional array from string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse query string in JavaScript
I want to create an options array from a string. How can i create an array as
{
width : 100,
height : 200
}
from a string like
'width=100&height=200'
Is there to create such arrays?
That's not an array, it's an object. It's not multidimensional either.
But anyway, you can split the string on the & separator and then split each item on the =:
var input = 'width=100&height=200',
output = {},
working = input.split("&"),
current,
i;
for (i=0; i < working.length; i++){
current = working[i].split("=");
output[current[0]] = current[1];
}
// output is now the object you described.
Of course this doesn't validate the input string, and doesn't cater for when the same property appears more than once (you might like to create an array of values in that case), but it should get you started. It would be nice to encapsulate the above in a function, but that I leave as an exercise for the reader...
Try this:
JSON.parse('{"' + decodeURI(myString.replace(/&/g, "\",\"").replace(/=/g,"\":\"")) + '"}')

empty jquery element [duplicate]

This question already has answers here:
Getting an empty JQuery object
(4 answers)
Closed 8 years ago.
Is there an elegant way to create an empty jquery element (versus null) ?
$myEmptyElement = $("#ThisIdDoesNotExist234343");
The rational is not checking later on for null.
Later we do :
$myEmptyElement.destroy();
Sure:
var $emptyElement = $();
Why would you want one, though?
A simple example would be:
var $emptyElement = $();
One way:
var $emptyElement = new $;

Categories

Resources