This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you pass a variable to a Regular Expression JavaScript?
I need help to resolve the issue because the code fails due to
numLength[0]:
<html>
<body>
<script type="text/javascript">
function xyz(){
var predefLength = "5,5";
var number = "20";
var numLength = predefLength.split(",");
var result = /^[-+]?\d{0,numLength[0]}(\.\d{0,numLength[1]})?$/.test(number);
document.write(result);
}
</script>
</body>
</html>
Any help will appreciated.
Use the RegExp-constructor, like:
var pattern = new RegExp('pattern as a string', 'flags as a string');
Or:
var result = new RegExp('^[-+]?\d{0,' + numLength[0] + '}(\.\d{0,' + numLength[1] + '})?$').test(number);
try this:
<script type="text/javascript">
function xyz(){
var predefLength = "5,5";
var number = "20";
var numLength = predefLength.split(",");
var pattern = new RegExp('^[-+]?\\d{0,'+numLength[0]+'}(\\.\\d{0,'+numLength[1]+'})?$');
var result = pattern.test(number);
document.write(result);
}
</script>
numLength[0] is 5. We can not add evaluation of variable in regular expression. Below code returns true.
var result = /^[-+]?\d{0,x}(\.\d{0,5})?$/.test(number);
Related
This question already has answers here:
Javascript toFixed localized?
(4 answers)
Closed last month.
Hey dear Stackoverflow community,
I have a JS code that calculates various values based on an input value. It is output with a dot, but it should be displayed with a comma.
How can I customize the code for this?
<script>
var input = document.getElementById("invest_input-1");
input.oninput = function() {
var out = ((input.value * 0.38));
document.getElementById('invest_output_result-1').innerHTML = out.toFixed(2);
var out = ((input.value * 0.032));
document.getElementById('invest_output_result-2').innerHTML = out.toFixed(2);
var out = ((input.value * 0.03));
document.getElementById('invest_output_result-3').innerHTML = out.toFixed(2);
var out = ((input.value * 0.13));
document.getElementById('invest_output_result-4').innerHTML = out.toFixed(2);
};
</script>
Thank you so much!
Kind regards, Fabian
Based on the user's locale:
new Intl.NumberFormat().format(3500)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#basic_usage
You can use
out.toFixed(2).replace('.', ',');
This question already has answers here:
How to add bold text using javascript
(3 answers)
Closed 1 year ago.
So i have this array of question written in js. And i access to my html using this:
const questionIndex = availableQuestion[arrayReady[currentPage-1]];
currentQuestion = questionIndex;
questionText.innerHTML = currentQuestion.q;
how can i make a certain word like = "not","except" from currentQuestion.q bold?
You need a list of words to be bolded, iterate over them, find them in currentQuestion.q, and then bold them.
const currentQuestion = {}
currentQuestion.q = "This is not a not really except good question."
console.log(currentQuestion.q)
const boldWords = ["not", "except"]
boldWords.forEach(word => {
currentQuestion.q = currentQuestion.q.replace(new RegExp("(" + word + ")", "g"), makeBold("$1"))
})
console.log(currentQuestion.q)
function makeBold(str) {
// your bold implementation
return str.bold()
}
I viewed all the answers, but I have developed a more browser compatible code compared to the already existing answers. Please have a look below.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display a string in bold.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
const str = "Hello Javesh, How are you doing today?"; //your question
const result = str.split(" ");
const keywords = ["Hello", "How"]; // your keywords
for(var i = 0; i<result.length; i++){
if(keywords.includes(result[i])){
document.querySelector("#demo").innerHTML += "<b>"+result[i]+"</b>"+" ";
}else{
document.querySelector("#demo").innerHTML +=result[i]+" ";
}
}
}
</script>
</body>
</html>
The bold() function is depreciated as its functionality can vary per browser.
Here's the MDN reference for the bold() function:-
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/bold
You could use the bold() function and combine it with a loop and conditions
You could use bold() <- (that's a function) and use it with loops/conditions
This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
How to force addition instead of concatenation in javascript [duplicate]
(3 answers)
Closed 3 years ago.
I have the following error:
jquery-3.4.1.min.js:2 The specified value "24.164.83" is not a valid
number. The value must match to the following regular expression:
-?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)?
Code
var grossTotal = netPrice * vatTotal // Is OK - it multiplies values
but
var grossTotal = netPrice + vatTotal // It makes this error -
it doesn’t sum.
The easiest way to produce a number from a string is prepend with +
var grossTotal= +netPrice + +vatTotal;
Try this code:
var netPrice = 1.432;
var vatTotal = 14.423;
var grossTotal = parseFloat(netPrice) + parseFloat(vatTotal)// Change according to data type of netPrice and/or vatTotal
console.log(grossTotal);
Try with type conversion:
var answer = parseInt(netPrice ) + parseInt(vatTotal);
You can use this code, First format value using parseFloat
var netPrice = 2.3777;
var vatTotal = 1.3777;
var grossTotal = parseFloat(netPrice ) + parseFloat(vatTotal);
console.log(grossTotal);
var netPrice = 2;
var vatTotal = 1;
var grossTotal = parseInt(netPrice ) + parseInt(vatTotal);
console.log(grossTotal);
For more information about parseFloat
https://www.w3schools.com/jsref/jsref_parsefloat.asp
This question already has answers here:
How to use split?
(4 answers)
Closed 7 years ago.
I have a string like this 132+456 or 132-456 or 132*456..etc ,it changes dynamically but I need to split this into 132 and 456 how to do it using pure java script?
It should be as easy as:
var parts = '132+456'.split('+');
parts[0]; //132
parts[1]; //456
Like so
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
// document.writeln only for example
document.writeln(a);
document.writeln(b);
Java
String[] values='132+456'.split('+');
String firstPart=value[0]; // has 132
for js
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
This question already has answers here:
Accessing variables from other functions without using global variables
(10 answers)
Closed 8 years ago.
I am trying to compute the total cost and using alert to display the total cost when the user hit the Submit button. I don't know how to call a var from other functions. Is there a way to do it? Any help will be appreciated. Thank you!
<script type = "text/javascript">
function CostCalculate(){
var TotalCost = AppleCost + OrangeCost + BananaCost;
alert("Your total cost is $ " + TotalCost);
}
function getQuantity1(){
var promptNum = prompt("Enter numbers of Apple: ");
var AppleNum = parseInt(promptNum);
var AppleCost = AppleNum * 0.59;
}
function getQuantity2(){
var promptNum = prompt("Enter numbers of Orange: ");
var OrangeNum = parseInt(promptNum);
var OrangeCost = OrangeNum * 0.49;
}
function getQuantity3(){
var promptNum = prompt("Enter numbers of Banana: ");
var BananaNum = parseInt(promptNum);
var BananaCost = BananaNum * 0.39;
}
Helo, I think you should read something about the variables scope in javascript.
https://msdn.microsoft.com/en-us/library/ie/bzt2dkta(v=vs.94).aspx
You can't acces a variable that's declarated in another function. What you can do is defining the variable outside of the functions.