How to target a specific part of string? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
returns the index where pattern starts in string (or -1 if not found).
The search is to be case sensitive if the 3rd parameter is true otherwise it is case insensitive.
Examples
index("abAB12","AB",true) returns 2 but index("abAB12","AB",false) returns 0
index("abAB12","BA",true) returns -1 and index("abAB12","BA",false) returns 1

Maybe something like:
var str = "abAB12";
var i = str.indexOf("AB");
if (i < str.length / 2) {
//stuff
} else {
//other stuff
}

Related

How can I extract this exact string in regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to get only the value of "vp" from the query string only if it is between 0-4. If it is bigger (for example: 5, 12 etc.) I want to get undefined.
"?vp=324" // ==> undefined
"?vp=1&mn=345" // ==> 1
"?mn=345&vp=2" // ==> 2
"?sf=23&vp=12&fd=343" // ==> undefined
May or may not need to parseInt for this.
function smallVP(queryString) {
let params = new URLSearchParams(queryString);
let vp = parseInt(params.get("vp"), 10);
if (vp >= 0 && vp <= 4) {
return vp;
}
}

How to split the number in js [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have only one number like "100000"
I wan't to split this number same as [20000,20000,20000,20000,20000] in array
so if number is 138000 then it should be like [20000,20000,20000,20000,20000,20000,18000]
How to do this?
let num = 118000,
num_array = [];
while (num > 0) {
if (num >= 20000) {
num_array.push(20000);
num -= 20000;
} else {
num_array.push(num);
break;
}
}
console.log(num_array);
I think this is what you want, please post what you tried, so that it helps to understand where you failed.

Find an element that has a text value of 0? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to find an element that's text value is 0.
I do:
var d = $('.content').find('div');
if(d.text().length === 0){
//do something
}
Is there a way to put the above logic in the selector?
I've tried:
var d = $('.content').find('div:empty');
But no luck as the div may have other empty html tags in.
Use a filter
var elems = $('.content').find('div').filter(function() { return $(this).text() == 0 });

Simple Javascript validation function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I do not have any JavaScript experience and would like some assistance in creating a function as I am not sure on how to do it.
I would like to create a Function that validates that x between 1 and 17 it can also be equal to 1 and 17
If the value is not valid a simple messagebox/alert could be used to notify the user
I know this is a really stupid question but thank you in advance
A function like that would look something like:
function validateNumber(n) {
var ok = n >= 1 && n <= 17;
if (!ok) alert('The value is not valid. Values from 1 to 17 are allowed.');
return ok;
}
You can call it, and you get the status back if you want to use it for something:
if (validateNumber(x)) {
// the number was valid
} else {
// the number wasn't valid
}

regular expression in javascript checking for "asdasd<something>asdsada" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how would I check for string
<something> in var string = "some chars <something> somechars"
You can use String.match():
var res = string.match(/<something>/g); // ["<something>"]
If there is no match, the value of res will be null. See example on JSFiddle.

Categories