This question already has answers here:
What are the Alternatives to eval in JavaScript?
(11 answers)
Closed 8 months ago.
console.log(Math.sqrt(4));
let str = 'Math.sqrt(4)';
console.log(str);
Is there any way for the 2nd console.log to show 4?
Is there any function which can do this?
You can use eval(), like
let str = eval('Math.sqrt(4)');
To do this, you can use the eval() function to evaluate the code in the string. Like this:
let str = 'Math.sqrt(4)';
console.log(eval(str));
Related
This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 3 years ago.
Is there a way to use the below JavaScript code to use comma's? For example the var num = 1924.00 Is there way to get it to display as 1,924.00?
var num = parseFloat(totalAmount).toFixed(2);
You could use a specific regex like this one in the function below:
function format_currency( value ) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
If you want to use it in conjuction with the .toFixed(), just do like this:
format_currency( 1245.3.toFixed(2) );
Hope it helps.
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 3 years ago.
I have a simple string like this, let string = "123½"; and I want to replace it.
I've already tried string.replace("½","0.5")
I'm not exactly what is happening here, but maybe it's because it's a special character?
string.replace doesn't modify a string; it returns a new string. Try:
const replaced = string.replace("½","0.5");
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 3 years ago.
I am playing with regular expressions in Javascript and have stumbled across an unexpected behaviour. Take a look: https://jsfiddle.net/ft7h5cw0/
I am trying to replace .html-wrap string in a #my-html-wrapper-random .html-wrap .main-content with #my-html-wrapper-random .html-wrap. For some reason, the result after replacement is:
#my#my-html-wrapper-randomper-random .html-wrap .main-content instead of expeceted #my-html-wrapper-random #my-html-wrapper-random .main-content
What is the reason for this behaviour?
var testString = "#my-html-wrapper-random .html-wrap .main-content";
var rxString = ".html-wrap";
var rx = new RegExp(rxString);
var result = testString.replace(rx,"#my-html-wrapper-random"); // unexpexted result strinf
console.log(result);
This question already has answers here:
How to find a number in a string using JavaScript?
(9 answers)
How can I extract a number from a string in JavaScript?
(27 answers)
Closed 4 years ago.
I have string in the bellow format:
let str = "url(#123456)";
My string have only number contain in it. It can be any where.
I want to extract the number 123456 from the above string.
I am using es6.
str.replace(/[^0-9]/g, '')
let str = "url(#123456)";
console.log(str.replace(/[^0-9]/g, ''))
another way to do it
let str = "url(#123456)";
console.log(str.match(/\d+/)[0])
I did it in a way too complicated way, but it does not involve regex so it's probably better to understand:
let str = "url(#123456)";
str = str.split("#");
str = str[1].split(")");
str = str[0];
console.log(str);
Using only Array methods:
console.log("url(#123456)".split('#').pop().slice(0, this.length -1))
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Parsing an URL in JavaScript
(3 answers)
Closed 8 years ago.
How to format a regular expression In JavaScript :
http://localhost:3000/sales?&tp=Home+Stay&am=Pool&pl=1620&pt=Flash+Sale&st=5
I want to get &pl= and all the digits after &pl=.
So the result would be &pl=1620 in this case.
Please help how to perform this??
Your help means a lot for me.
This script will do the trick:
var regex = /&pl=\d*/;
var match = regex.exec(yourtext)[0];
Following regex should do it
/&pl=\d*/.exec('http://localhost:3000/sales?&tp=Home+Stay&am=Pool&pl=1620&pt=Flash+Sale&st=5')[0]
Demo: Fiddel