Need to replace numbers between [...] with Javascript or jQuery [closed] - javascript

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 6 years ago.
Improve this question
Inside a string: I need to replace the number between [ ] by another number with Javascript or jQuery.
What is the best way to proceed ?
Eamples
"[12345].Checked" > "[1].Checked"
"[123].RequestID" > "[21].RequestID"
Thanks for your help.

function fixNum(str, newnum) {
return str.replace(/\[\d+\]/,'[' + newnum + ']')
}

Use .replace:
"[12345].Checked".replace(/\[(\d+)\]/, "[1]") // "[1].Checked"

With regular expressions:
/\[(\d+)\]/
It searches a number (no matter length) inside a [] set.
Combined with replace you can make it.
Test it:
https://regex101.com/r/zT5kD0/1

Use replace method to replace the part
function replaceStr(string,orgText,replaceText){
var res = string.replace(orgText, replaceText);
return res;
}
console.log(replaceStr("[12345].Checked",'12345','1'));
jsfiddle

Related

Can someone explain to me about array.forEach() [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 4 months ago.
Improve this question
I have explore a few webs and watch some videos but I couldn't understand how array.forEach() works
This is the script that I found regarding that
let meals = ["rice", "meat", "salad"];
meals.forEach(capitalize);
meals.forEach(print);
function capitalize(element, index, array){
array[index] = element[0].toUpperCase() + element.substring(1);
}
function print(element){
console.log(element);
}
Can someone explain to me about how array[index] = element(0).toUpperCase() + element.substring(1); and meal.forEach(); works?
forEach() is an array function from JavaScript, that is used to iterate over items in a given array.
element[0] will return first character of each array element.
toUpperCase() is function use to convertthe string into upper case.
element.substring(1) this will return a string, except for the first character.

Returning query fragments [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 4 years ago.
Improve this question
I have an url that looks like this:
https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en
Is it possible to return every dk parameter separately? (no matter if there will be 1 or 5 dk parameters) so i would get separately sports, groupcompanyinsider, local.
If its not possible maybe there is a way to return all of them in one string like dk=sports&dk=groupcompanyinsiderlocal&dk=local ?
You can use the built-in javascript class URLSearchParams for this.
You can then transform this into the string you want with string concatenation and a foreach.
const url = "https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en";
var params = new URLSearchParams(url);
var result = "";
// concatenate individual values of the 'dk' query parameter
params.getAll('dk').forEach(function (item) {
result += '&dk=' + item;
});
result = result.substr(1); // remove starting '&' from the result;
console.log(result);
The result should contain your desired string.

javascript remove html tags and replace html entities [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 5 years ago.
Improve this question
In javascript, using regex, what is the most efficient way to take HTML as an input, and return just the text it contains, without the HTML tags and with all HTML entities replaced with the actual character they represent - eg '#nbsp;' would be replaced with ' ', and '#lt;' would be replaced with '<'?
Thank you
You don't want to use regex for that. It sounds like you could just do:
function get(html) {
var el = document.createElement( 'html' );
el.innerHTML = html;
return el.textContent;
}
var data = get('<strong>Test Hello <3');
console.log(data); //Test  Hello <3

how to pass a variable to queryselectorAll? [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 5 years ago.
Improve this question
Hi I want to pass a variable to queryselectorAll. I tried a couple of things but non have worked for me. I'm doing that because in my code I change the variable to a new one every time a click a button.
var container = document.getElementById(containerBox.id)
// I want containerBox.id to be called in querySelectorAll
templateDiv = document.querySelectorAll('#' + 'containerBox.id' + 'template')[0].content.firstElementChild
Thanks in advance
This question can be simplified to: How do I construct a string from fixed parts and a variable part?
The answer to that is:
'#' + containerBox.id + 'template'
(i.e. just don't put quotes around your variable name).
But why bother using .querySelectorAll() just to grab the first index? You could simply call .querySelector() instead.
And if all you need to get is an element with a specified id, you can just do this:
document.getElementById(containerBox.id + 'template')
... which is the method you're already using in your first line.

how to insert variable into jquery selector [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
I need to insert a variable inside a jQuery selector like this example, how can I fix my problem?
var index = 5;
$(".tables li:nth-child('+index+')").addClass('vr_active');
The issue is in the quotes ...
var index = 5;
$(".tables li:nth-child("+index+")").addClass('vr_active');
This way, the index variable is part of a concatenation of:
".tables li:nth-child(" ... a string
index ... a variable
")" ... a string.
Functioning jsFiddle
try to use .eq()
var index = 5;
$(".tables > li").eq(index - 1).addClass('vr_active');
A jQuery selector is just a string.
So your problem is actually "How to insert a variable into a string" and this can be done in a myriad of ways, for example:
".tables li:nth-child(" + index + ")"
var selector = ".tables li:nth-child(${index})";
selector = selector.replace("${index}", index);
etc.

Categories

Resources