This question already has answers here:
Why does a RegExp with global flag give wrong results?
(7 answers)
Closed 3 months ago.
Can someone help check this snippet? I'm just looping on array but the last item return false or null even its matches the pattern.
let texts = ["Invoice_001_1", "Invoice_001_2"];
var regTest = /(([a-zA-Z 0-9]+)_([0-9]+)_([0-9]+)$|[a-zA-Z ]+_([0-9]+)$)/gi;
for(var x=0; x<texts.length; x++) {
console.log(texts[x]);
var matchReg = regTest.exec(texts[x]);
console.log(matchReg);
}
JSfiddle Demo
The issue is that when using /g, js has a little gotcha, in that it tells the regexp to keep the index of the last match.The fix is to manually reset the index:
let texts = ["Invoice_001_1", "Invoice_001_2"];
var regTest = /(([a-zA-Z 0-9]+)_([0-9]+)_([0-9]+)$|[a-zA-Z ]+_([0-9]+)$)/gi;
for(var x=0; x<texts.length; x++) {
console.log(texts[x]);
var matchReg = regTest.exec(texts[x]);
console.log(matchReg);
regTest.lastIndex = 0;
}
Related
This question already has answers here:
Mulitple Elements with the same ID
(5 answers)
Closed 2 years ago.
I have the following code to change the color of text with a certain spanID. Currently, it only changes the first instance of the span and on subsequent instances. Any suggestions?
<script>
function spanColor() {
var x = document.getElementById('someId');
x.style.color = '#'+Math.random().toString(16).substr(-6);
}
</script>
If you want to return all the needed elements you should use querySelectorAll(#id)
But as you saw in the comments - ID is not the best way to get more than one item.
You should change the ID for a ClassName and use var x = document.getElementsByClassName("example");
So your code should look like this:
<script>
function spanColor() {
var x = document.getElementsByClassName('someClassname');
var i;
for (i = 0; i < x.length; i++) {
x[i].style.color = '#'+Math.random().toString(16).substr(-6);
}
}
</script>
This question already has answers here:
JavaScript, getting value of a td with id name
(8 answers)
Closed 2 years ago.
I wrote some code that is scanning my HTML table, and I want to use it for formatting that table.
var count_rows = document.getElementById("currency_tab").rows.length;
for (i = 0; i <= count_rows; i++ ) {
var count_cells = document.getElementById("currency_tab").rows[i].cells.length;
for (j = 0; j <= count_cells; j++) {
var check_str = document.getElementById("currency_tab").rows[i].cells[j];
/*
console.log(check_str);
console.log(typeof(check_str));
*/
var check = check_str.includes("-")
if(check) {
check_str.style.color = "red";
} else {
check_str.style.color = "green";
}
}
}
js console.log(check_str); is returning not a value of cell but an object e.g. <th>CURRENCY</th>.
I have tried to parse it with check_str.slice but that is forcing me to count a length of chars in object. I hope there is easier method to resolve that.
You can get the text with check_str.textContent
Please refer to the following documentation: Node.textContent
Also, if you are unsure about the properties of an object you can log them with console.dir(check_str).
This question already has answers here:
Reference a javascript property with a dynamic name
(3 answers)
Closed 3 years ago.
Currently I'm trying out some html and js stuff.
I have trouble using a changing string inside a command with some "." in it.
for (let i = 1; i <= 2; i++) {
let count = String("q" + i);
console.log(document.calculate.$(this.count).value);
}
So the String "count" is changing and I want the different values of my document with the names "q1", "q2"...
Would be awesome if someone could help me.
Thank You!
put dinamic value in a [] quotes instead .
console.log(document.calculate[$(this.count)].value);
for (let i = 1; i<=2; i++) {
let count = String("q" + i);
console.log(document.calculate.$(this[count]).value);
}
This question already has answers here:
JSlint: unexpected 'for' [duplicate]
(2 answers)
Closed 7 years ago.
I'm new to JSLint and I'm trying to create function that outputs amount of elements specified in first argument. Normally I would use the for loop but JSLint doesn't like loops and complains about it.
I've searched the web looking for satisfying answer, but the only ones that I've found are with use of new Array or other way of outsmarting JSLint.
So, how to change this code to JSLint-friendly?
function createElements(amount) {
var i;
var elements = [];
for (i = 0; i < amount; i += 1) {
elements.push(document.createElement('div'));
}
return elements;
}
Try this code,
function createElements(amount, document) {
'use strict';
var i = 0;
var elements = [];
while (i < amount) {
i = i + 1;
elements.push(document.createElement('div'));
}
return elements;
}
This question already has an answer here:
Set String via String.prototype function without return
(1 answer)
Closed 8 years ago.
At the starting I must tell that I am a newbie and sorry for a such a silly question
Second that .extract is just an example.
Here is the code
String.prototype.extract=function(start,end){
var value=this;
var rval="";
for(var i=start; i<=end; i++){
rval+=value.charAt(i)
}
????? = rval;
}
var str="abcdef";
str.extract(1,4)
alert(str) //must be bcde
Now I want the caller of method extract (str) and the value of rval must be saved to that only.
I could do it by:
str = str.extract;
and in the function return rval
but I want that like we do it as something.toUpperCase instead of something=something.toUpperCase
????? must be replaced by the caller of method extract (str)
any help would be useful
The best way to handle this is with a callback.
Re-using your code, it would look like:
String.prototype.extract=function(start,end, callback){
var value=this;
var rval="";
for(var i=start; i<=end; i++){
rval+=value.charAt(i)
}
callback(rval);
}
var str="abcdef";
str.extract(1,4, function(s1) {
alert(s1) //must be bcde
});
Another way to handle it would be to simply return the value and re-assign it to the original variable.
String.prototype.extract=function(start,end, callback){
var value=this;
var rval="";
for(var i=start; i<=end; i++){
rval+=value.charAt(i)
}
return rval;
}
var str="abcdef";
str = str.extract(1,4);
alert(str) //must be bcde