error with usage of variables in regular expression (match) in javascript - javascript

what is going wrong with the below code?
Even if I pass the value as "aa" or "a", matchingArray is null.
Probably something is going wrong with the usage of variable in the regular expression.
var gEnLowercase = "a-z";
var gLanguageLowercase = "([" + gEnLowercase + "]";
gLanguageLowercase = gLanguageLowercase + "{0 , " + aLength + "})";
var filter = new RegExp(gLanguageLowercase);
var matchingArray = aValue.match(filter); // filter value => /([a-z]{0,10})/

remove the extra space after 0 in the gLanguageLowercase ...it should be
gLanguageLowercase = gLanguageLowercase + "{0," + aLength + "})";

var gEnLowercase = "a-z";
var gLanguageLowercase = "([" + gEnLowercase + "]";
gLanguageLowercase = gLanguageLowercase + "{0," + 3 + "})";
var filter = new RegExp(gLanguageLowercase);
console.log(filter);
var matchingArray = "sds".match(filter);
console.log(matchingArray);
remove the extra spaces

You have to use filter.exec(aValue); instead of aValue.match(filter); Besides there are extra spaces in your regexp.
var gEnLowercase = "a-z";
var aLength = 10;
var gLanguageLowercase = "([" + gEnLowercase + "]";
gLanguageLowercase = gLanguageLowercase + "{0," + aLength + "})";
var filter = new RegExp(gLanguageLowercase);
var matchingArray = filter.exec(aValue); // filter value => /([a-z]{0,10})/

Related

Replacing null with 0 when summing values

I have a function that sums my values and everything works fine, but only when all inputs have a value entered. However, I would like default to have 0 assigned to it.so that the function works when at least one value is given . How to do it ?.
var DeductiblepercentageM = thisPointer.entity.getValue('number.DeductiblepercentageM[' + i + ']');
var InsuranceLimitM = thisPointer.entity.getValue('number.InsuranceLimitM[' + i + ']');
var insuranceRaitingM = thisPointer.entity.getValue('number.insuranceRaitingM[' + i + ']');
var InsurerNumberM = thisPointer.entity.getValue('number.InsurerNumberM[' + i + ']');
DeductiblepercentageM = DeductiblepercentageM.replace(",", ".");
DeductiblepercentageM = parseFloat(DeductiblepercentageM)
InsuranceLimitM = InsuranceLimitM.replace(",", ".");
InsuranceLimitM = parseFloat(InsuranceLimitM)
insuranceRaitingM = insuranceRaitingM.replace(",", ".");
insuranceRaitingM = parseFloat(insuranceRaitingM)
InsurerNumberM = InsurerNumberM.replace(",", ".");
InsurerNumberM = parseFloat(InsurerNumberM)
//log the outcome of decimal separator change
var positionSum = +(DeductiblepercentageM + InsuranceLimitM +insuranceRaitingM + InsurerNumberM);
jQ('[id="DeductibleM[' + i + ']"]').val(positionSum);
thisPointer.entity.setValue('DeductibleM[' + i + ']', positionSum);
thisPointer.entity.mergeLocal(true);
if (totalSum != "NaN") {
totalSum = +(totalSum + positionSum).toFixed();
}
}
else {
totalSum = "-";
}
According to #Terry
var InsuranceLimitM = thisPointer.entity.getValue('number.InsuranceLimitM[' + i + ']') || 0;
adding || 0 to the end of the code helps and makes the code count right away

Done / How to make a space printer in javascript

var arg = 5
var string = ' '
for (let i = 0; i < arg; i++) {
console.log('"' + string + '"')
}
I expected the output is:
" " \\ There are 5 spaces between the ""
But the output is:
" "
" "
" "
" "
" "
I am a newbie in javascript. Hope you will help me
you can use method string.padEnd that fill string with blank space until parameter pass to the method
var arg = 5;
var string = ' ';
string = string.padEnd(arg)
console.log('"' + string + '"');
reference : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
I don't think there is a way to console.log on the same line. You can do something like this
var arg = 5
var string = ' '
var final_string = ''
for (let i = 0; i < arg; i++)
final_string = final_string + string
}
console.log('"' + final_string + '"')
I think you can use or \xa0 for printing spaces
var arg = 5
var string = '\xa0\xa0\xa0\xa0\xa0';
for (let i = 0; i < arg; i++) {
console.log('"' + string + '"')
}
There are many ways to do this. One would be to do it via a loop. That way you can stay flexible. For loop has already been mentioned. here with a foreach loop.
let string = '';
[...Array(5).keys()].forEach(e => string += ' ');
console.log('"' + string + '"');

JavaScript not reading variable value in else code

I am trying to print the log for following JavaScript. However, script is not printing the log value. I highlighted the variable value the script should identify in the if/else code, however after else it is not reading the $systemDate from outside the if/else statement; as a result it is not logging the value for $dueDate. I am trying following code:
_wait(_div("/date-display .* bwc-selected/"));
var $date = _getText(_div("/date-display .* bwc-selected/"));
_wait(2000);
_log($date);
_wait(2000);
var str = ($date)
var d = new Date(str)
var $systemDate = (d.getUTCMonth()+1) +"/"+ d.getUTCDate() + "/" + d.getUTCFullYear();
_log('System Date is:' + $systemDate);
var $dueDate = person.$dueDate;
if ($dueDate != null)
{
var $dueDate = person.$dueDate;
person.$dueDate = $dueDate;
_log('Due Date is:' + $dueDate);
}
else
{
var $days = 90;
var $theDate = new Date($systemDate);
var $pregnancyDueDate = new Date($theDate);
$pregnancyDueDate.setDate($pregnancyDueDate.getDate() + $days);
var due = ($pregnancyDueDate);
var n = new Date(due);
var $dueDate = (n.getUTCMonth()+1) +"/"+ n.getUTCDate() + "/" + n.getUTCFullYear();
_log('Due Date is:' + $dueDate);
person.$dueDate = $dueDate;
}
Do not redeclare your variable !
remove var from
var $dueDate = (n.getUTCMonth()+1) +"/"+ n.getUTCDate() + "/" + n.getUTCFullYear();
it should be:
$dueDate = (n.getUTCMonth()+1) +"/"+ n.getUTCDate() + "/" + n.getUTCFullYear();
You variable was already declared here before your if
var $dueDate = person.$dueDate;
and finally update your if condition to :
if ($dueDate != "")
because your cells can be just empty and not containing null values

cant figure out how to get the return showing .toFixed(2) amount without error

The script works great for adding items on invoice however i cant figure how to convert the data using .toFixed(2) to show $10.00 instead of 10. I get an error every time I try to add .toFixed(2) . thank you
<script type="text/javascript">
function myFunction() {
var answer = document.getElementById('total');
var x = document.getElementById('itemprice1');
var y = document.getElementById('itemprice2');
var z = document.getElementById('itemprice3');
var w = document.getElementById('itemprice4');
var taxt = document.getElementById('taxtot');
var thetot = document.getElementById('thetot');
// parseFloat converts to values, otherwise you'll concatenate the strings.
answer.value = parseFloat("0" + x.value) + parseFloat("0" + y.value) + parseFloat("0" + z.value) + parseFloat("0" + w.value);
}
function myFunction1() {
var answer = document.getElementById('total');
var taxt = document.getElementById('taxtot');
var thetot = document.getElementById('thetot');
thetot.value = parseFloat("0" + answer.value) + parseFloat("0" + taxt.value);
if (thetot > "0") {
{
//function myFunction2()
var taxt = document.getElementById('taxtot');
var tx1 = document.getElementById('tax1');
var tx2 = document.getElementById('tax2');
var tx3 = document.getElementById('tax3');
var tx4 = document.getElementById('tax4');
var x = document.getElementById('itemprice1');
var y = document.getElementById('itemprice2');
var z = document.getElementById('itemprice3');
var w = document.getElementById('itemprice4');
var answer = document.getElementById('total');
taxt.value = parseFloat("0" + tx1.value) * ("0" + x.value) + parseFloat("0" + tx2.value) * ("0" + y.value) + parseFloat("0" + tx3.value) * ("0" + z.value) + parseFloat("0" + tx4.value) * ("0" + w.value);
}
}
}
</script>
Presumably your controls are in a form, so you can reference them simply using their name in the form. You can also convert strings to numbers using unary +:
function myFunction(formId) {
var f = document.getElementById(formId);
var answer = f.total;
var x = +f.itemprice1.value;
var y = +f.itemprice2.value;
var z = +f.itemprice3.value;
var w = +f.itemprice4.value;
var taxt = f.taxtot;
var thetot = f.thetot;
// Presumably here is where you want to use toFixed
answer.value = '$' + (x + y + z + w).toFixed(2);
}
function to_dollar_string(amount)
{
return "$" + amount.toFixed(2);
}
to_dollar_string(10);
=> "$10.00"
to_dollar_string(10.567);
=> "$10.57"

getting statistics from a textarea

Getting text statistics from a textarea.
which would be better?
this one?
function getStats() {
var text = textarea.value,
stats = {};
stats.chars = text.length;
stats.words = text.split(/\S+/g).length - 1;
stats.lines = text.replace(/[^\n]/g, "").length + 1;
return stats.lines + " lines, " + stats.words + " words, " + stats.chars + " chars";
}
or this one?
function getStats() {
var text = textarea.value,
chars = text.length,
words = text.split(/\S+/g).length - 1,
lines = text.replace(/[^\n]/g, "").length + 1;
return lines + " lines, " + words + " words, " + chars + " chars";
}
The second one.
Not for any performance reasons, but you are just declaring a Javascript object when there is no need for one.
Creating an object to store your variables would only make sense if you were using it like:
function getStats() {
var text = textarea.value,
stats = {};
stats.chars = text.length;
stats.words = text.split(/\S+/g).length - 1;
stats.lines = text.replace(/[^\n]/g, "").length + 1;
return stats;
}

Categories