Using the anonymous function in String.replace() method [duplicate] - javascript

This question already has answers here:
Replace function not replacing [duplicate]
(2 answers)
Closed 10 years ago.
I have an array of regular expressions, at the moment just one element in length, and a string which I want to search/replace by looping through the array:
var str = "i am*happy all the*time",
rex = [
/(\S)\s*\*\s*(\S)/g
],
i = 0,
r = rex.length;
This is how I'm trying to achieve this at the moment:
for (i; i < r; i += 1) {
str.replace(rex[i], function(star, p1, p2) {
console.log(i, star, p1, p2);
return p1 + '\\s*(.*)\\s*' + p2;
});
}
The result should be i am*\\s*(.*)\\s*happy all the\\s*(.*)\\s*time. But at the moment, str seems unaffected, even though when I check the console the relevant matches are being made. You can see this for yourself here.
So am I missing something simple, have I misunderstood something about using lambda expressions in String.replace(), or is there something more fundamentally wrong here?
...
EXTRA INFO:
I'm using Chrome 24 right now, in case that's of interest; I had read a while ago that anonymous functions in String.replace() weren't available to all browsers though I assumed that would be resolved by now (the option was introduced in ECMAScript v3).

String.replace() doesn't change the original string, but returns the new one. You should assign the result
for (i; i < r; i += 1) {
str = str.replace(rex[i], function(star, p1, p2) {...})
}

Related

Javascript variable setting values, trying to understand meaning [duplicate]

This question already has answers here:
Using comma in Javascript variable declaration [duplicate]
(2 answers)
Closed 1 year ago.
Can someone please explain the below code for me?
Is the line setting multiple variables at the same time? If so, what is are they?
If it is setting multiple variables, I would have expected all the items to be X=Y, but instead I get random variables within the line, like reevVal, mmareev, amareev, fvVal.
Any help is greatly appreciated.
var balance = $(".input-balance"), value = $(".balance-value"), reevVal, mmaVal = $(".mma-val span"), mmareev, amaVal = $(".ama-val span"), amareev, triggerPlus = "false", triggerMinus ="false", fvVal;
var x = 0, y, z = 1
is equivalent to
var x = 0;
var y; // undefined
var z = 1
There's also the similar, but different, comma operator that lets you "group" up multiple expressions into a single expression. The value of the last sub-expression is returned, but all sub-expressions are evaluated:
let x = (5, doSix(), 7); // x = 7
x = 8, doNine(), 10; // 10
Note the parens are necessary in the 1st line above to make it clear it's a single declaration and not a list of declarations like in the 1st example. The parens aren't necessary in the 2nd line, since there's not let|var|const so it's not a declaration, but an assignment.
In practice, it's most common in for loops where you're constrained to a single expression:
for (let i = 0, j = 10; i < j; i++, j--)
console.log(i, j);

Will if / else if / else become an expressions in typescript / javascript at some point?

I really like this feature in Kotlin:
In Kotlin, if is an expression, i.e. it returns a value.
// As expression
val max = if (a > b) a else b
(from https://kotlinlang.org/docs/reference/control-flow.html)
Is this also planned for typescript / javascript?
Update:
In reply to the ternary operator suggestions, of course I also mean cases with else if:
val bla = if ( 5 == 6) {
7
} else if (5 == 7) {
8
} else {
9
}
println(bla) // prints 9
Is this also planned for typescript / javascript?
No. Statements vs. Only Expressions is a pretty hard fork in language design and will not change without breaking existing code so will not happen.
Simple example:
var foo = 123;
function inc(){
foo = foo + 1;
}
Going down the expression route would change the return of this function from undefined to foo which can drastically break existing code for more complex functions.
It already exists Ternary Conditional

How do I create a javascript elite planet name generator? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I try to convert Tricky's script (that generates a name from elite) to javascript here:
https://github.com/rubo77/eliteNameGen/blob/master/elite.js
But I get stuck at this LPC-code by Tricky:
digrams=ABOUSEITILETSTONLONUTHNO..LEXEGEZACEBISOUSESARMAINDIREAERATENBERALAVETIEDORQUANTEISRION
...
pairs = digrams[24..<1];
...
names[0..<2]
I couldn't find a manual to LPC that would explain this syntax.
In the End I want to create a javascript, that creates a random planet name from the old C64 game Elite.
I also found a python version, (but that seems a bit more complicated to me)
Ok I managed to port the code over, but I had to tweak a bit of the algorithm. The one that is provide by Tricky, for some reason, produces non-unique names. I used the tweakseed function to tweak the seeds to generate a list of random names.
Answer
To answer the question above, #MattBurland is correct. You would replace the following code:
pairs = digrams[24..<1];
with
pairs = digrams.substring(24);
The following code, however, is actually printing out the list of names. So you're indexing an array - in which case:
names[0..<2]
becomes
for (var i = 0; i < (names.length - 2); i++) {
names[i]
}
Analysis
Just to give this some more depth. I've analyzed the code and realized that rotatel, twist, tweakseed, and next were just used to create random numbers. I don't know enough about LPC, but I think that at the time it probably didn't have a pseudo-random number generator.
A lot of this code can be removed and just replaced with Math.random. The key part of this entire program is the variable digram. This sequence of characters produces Alien-like names. I figure it probably has something to do with alternation of consonants and vowels. Grabbing them in pairs randomly will almost always produce some sort of consonant + vowel pairing. There is the odd time where you'll end up with a name like 'Rmrirqeg', but in most cases, the names appear Alien-like.
Port
Below is a direct port of the code. You can use this jsFiddle to see it in action, but it uses AngularJS to print out the names instead of printing out a list like the code provided. genNames will produce an array of names, which you can use for whatever reason you want.
Note this port only works on IE9+, since it uses map, reduce, and forEach. Replace these with loops if you plan on using this on IE8 or below.
You can tweak this to produce names longer or shorter. However, the length of the names is dependent on the pairs array. Either use Math.random or something to make it completely wild.
var digrams = "ABOUSEITILETSTONLONUTHNO" +
"..LEXEGEZACEBISOUSESARMAINDIREA.ERATENBERALAVETIEDORQUANTEISRION";
function rotatel(x) {
var tmp = (x & 255) * 2;
if (tmp > 255) tmp -= 255;
return tmp;
}
function twist(x) {
return (256 * rotatel(x / 256)) + rotatel(x & 255);
}
function next(seeds) {
return seeds.map(function(seed) {
return twist(seed);
});
}
function tweakseed(seeds) {
var tmp;
tmp = seeds.reduce(function(total, seed) {
return total += seed;
}, 0);
return seeds.map( function ( seed, index, arr ) {
return arr[index + 1] || (tmp & 65535)
});
};
function makename(pairs, seeds)
{
var name = [];
/* Modify pair if you want to have names shorter or longer than 8 chars */
/* I'll leave that as an exercise for you. */
var pair = [0, 0, 0, 0];
var longname = seeds[0] & 64;
pair = pair.map(function() {
seeds = tweakseed(seeds);
return 2 * ((seeds[2] / 256) & 31);
});
pair.forEach(function(value, index, arr) {
if (longname || ( index < (arr.length - 1))) {
name.push(pairs[value]);
name.push(pairs[value + 1]);
}
});
return name.join('').toLowerCase()
.replace(/^\w/, function(letter) {
return letter.toUpperCase();
});
}
function genNames()
{
var names = [];
var pairs;
var num = 256;
var seeds = [23114, 584, 46931];
pairs = digrams.substring(24);
while (--num) {
names.push( makename(pairs, seeds) );
seeds = tweakseed(next(seeds));
}
return names;
}
For the range operator in LPC, this link helps:
http://www.unitopia.de/doc/LPC/operators.html
expr1[expr2..expr3] Extracts a
piece from an array or string.
expr2 or expr3 may be omitted, default is the begin
or end of expr1.
Negative numbers for expr2 or expr3
mean ``count from before the beginning'', i.e.
foo[-2..-1] is an empty array or string.
foo[<2..<1] gives the 2nd and last element of
the array resp. chars of the string.
So I'm guessing that:
pairs = digrams[24..<1];
Means get the substring starting at index 24 to the end of the string?

Evaluate an Equation in Javascript, without eval() [duplicate]

This question already has answers here:
Evaluating a string as a mathematical expression in JavaScript
(26 answers)
Calculate string value in javascript, not using eval
(12 answers)
Safe evaluation of arithmetic expressions in Javascript
(5 answers)
How to code a calculator in javascript without eval
(1 answer)
Eval alternative
(4 answers)
Closed 5 days ago.
I have a bunch of fields in a web page (150+) that need to have equations run on them to produce a result.
I currently store the equation like this:
<input name="F7" type="text" class="numeric" data-formula="([C7]-[D7])/[E7]" readonly />
When an input is blurred, I use a jQuery selector to iterate over all inputs with a data-formula attribute, take the formula, and use regex to replace the pointers (the [C7] in the equation) with their appropriate values.
After that, I eval() the equation to get a result, and put it in the correct input. This works great, but is very slow and results in the web page hanging for a few seconds, which is bad if it happens every time an input is blurred.
Is there a way to evaluate an equation, such as "(1-2)/4", without using eval()? These equations also may have functions, such as square root (which makes eval() nice, since I can just put Math.sqrt() in the formula), and the numbers may be decimals.
Note: This application must run on IE7 and 8, so I don't believe I can use Webworkers or anything like that. I have also considered only running this code after a "Save" button is hit, but I would prefer the UI to update live if possible
I only really know two alternatives, one is to use a script element that is dynamically written to the page, e.g.:
function evaluate(formula)
{
var script = document.createElement("script");
script.type = "text/javascript";
script.text = "window.__lr = " + formula + ";";
document.body.appendChild(script);
document.body.removeChild(script);
var r = window.__lr;
return r;
}
The other would be to use new Function(...):
function evaluate3(formula)
{
var func = new Function("return " + formula);
return func();
}
But I don't think you'll find something that yields similar performance to eval: http://jsperf.com/alternative-evaluation
The performance of eval varies across browsers and platforms, have you got a specific browser/platform combination in mind? The newer javascript engines in improved browsers will offer optimised eval:
This is only a limited set of tests on a few UAs, but it should give you an idea of how it performs in different environments.
Is there a way to evaluate an equation, such as "(1-2)/4", without using eval()?
Well, you can tokenize the expression and write your own evaluator that mimics what eval does. But while that might be useful in terms of limiting the side-effects (since eval is a very big hammer), it's extremely unlikely to perform better than eval does.
What you can do, though, is cache the result of evaluating all the other inputs so that you only evaluate the input the actually blurred. That should be quite efficient indeed.
For example, suppose you had this global object:
var values = {
A7: /* initial value for A7 */,
B7: /* initial value for B7 */,
C7: /* initial value for C7 */,
D7: /* initial value for D7 */,
E7: /* initial value for E7 */,
F7: /* initial value for F7 */,
/* etc */
};
...and then attached this blur handler to all inputs:
$("input").blur(function() {
values[this.id] = this.value; // Or parseInt(this.value, 10), or parseFloat(this.value), etc.
doTheEvaluation();
});
...where doTheEvaluation used the values from values rather than recalculating all of them every time.
If this.value might refer to other fields, you could do a recursive evaluation of it — but without evaluating all of your inputs.
I do realize this answer is 8 years too late, but I thought I would add my own contribution since this issue came up in a project I was working on. In my case, I am using Nodejs, but this solution should work for a browser as well.
let parens = /\(([0-9+\-*/\^ .]+)\)/ // Regex for identifying parenthetical expressions
let exp = /(\d+(?:\.\d+)?) ?\^ ?(\d+(?:\.\d+)?)/ // Regex for identifying exponentials (x ^ y)
let mul = /(\d+(?:\.\d+)?) ?\* ?(\d+(?:\.\d+)?)/ // Regex for identifying multiplication (x * y)
let div = /(\d+(?:\.\d+)?) ?\/ ?(\d+(?:\.\d+)?)/ // Regex for identifying division (x / y)
let add = /(\d+(?:\.\d+)?) ?\+ ?(\d+(?:\.\d+)?)/ // Regex for identifying addition (x + y)
let sub = /(\d+(?:\.\d+)?) ?- ?(\d+(?:\.\d+)?)/ // Regex for identifying subtraction (x - y)
/**
* Evaluates a numerical expression as a string and returns a Number
* Follows standard PEMDAS operation ordering
* #param {String} expr Numerical expression input
* #returns {Number} Result of expression
*/
function evaluate(expr)
{
if(isNaN(Number(expr)))
{
if(parens.test(expr))
{
let newExpr = expr.replace(parens, function(match, subExpr) {
return evaluate(subExpr);
});
return evaluate(newExpr);
}
else if(exp.test(expr))
{
let newExpr = expr.replace(exp, function(match, base, pow) {
return Math.pow(Number(base), Number(pow));
});
return evaluate(newExpr);
}
else if(mul.test(expr))
{
let newExpr = expr.replace(mul, function(match, a, b) {
return Number(a) * Number(b);
});
return evaluate(newExpr);
}
else if(div.test(expr))
{
let newExpr = expr.replace(div, function(match, a, b) {
if(b != 0)
return Number(a) / Number(b);
else
throw new Error('Division by zero');
});
return evaluate(newExpr);
}
else if(add.test(expr))
{
let newExpr = expr.replace(add, function(match, a, b) {
return Number(a) + Number(b);
});
return evaluate(newExpr);
}
else if(sub.test(expr))
{
let newExpr = expr.replace(sub, function(match, a, b) {
return Number(a) - Number(b);
});
return evaluate(newExpr);
}
else
{
return expr;
}
}
return Number(expr);
}
// Example usage
//console.log(evaluate("2 + 4*(30/5) - 34 + 45/2"));
In the original post, variables may be substituted using String.replace() to provide a string similar to the example usage seen in the snippet.
I would modify your code to perform only one eval.
var expressions = []
// for each field
// expressions.push("id:" + parsedExpression);
var members = expressions.join(",");
var resultObj = eval("({" + members + "})");
// for each field
document.getElementById(id).value = resultObj[id];
Validation: I'd write a powerful Regular expression to validate the input, then use eval to evaluate it if it's safe.
Evaluation: Regarding the speed of eval: If it's a big problem, you could queue up all equations (store it in an array), and evaluate them all at once:
var equations = ['1+1', '2+2', '...']; //<-- Input from your fields
var toBeEvald = '[' + equations.join(',') + '];';
var results = eval(toBeEvald);
// result[0] = 2
// result[1] = 4, etc
If you had a reliable internet connection, you could connect to google and use their services to evaluate an expression. Google has a pretty powerful server, and all you would have to do is send a request with the queue being the equation and retrieve it. Of course, this could be slower or faster depending on internet speed/browser speed.
Or, you can write your own equation evaluator. This is pretty difficult, and probably won't be any more efficient than eval. You'd also have to go through the immense trouble of the PEMDAS order.
I suggest you could merge the equations together into one string, and eval that all at once, and retrieve the results all at once.
You can use new Function to evaluate your expressions

Javascript string replace with calculations

Is there a way to resolve mathematical expressions in strings in javascript? For example, suppose I want to produce the string "Tom has 2 apples, Lucy has 3 apples. Together they have 5 apples" but I want to be able to substitute in the variables. I can do this with a string replacement:
string = "Tom has X apples, Lucy has Y apples. Together they have Z apples";
string2 = string.replace(/X/, '2').replace(/Y/, '3').replace(/Z/, '5');
However, it would be better if, instead of having a variable Z, I could use X+Y. Now, I could also do a string replace for X+Y and replace it with the correct value, but that would become messy when trying to deal with all the possible in-string calculations I might want to do. I suppose I'm looking for a way to achieve this:
string = "Something [X], something [Y]. Something [(X+Y^2)/(5*X)]";
And for the [___] parts to be understood as expressions to be resolved before substituting back into the string.
Thanks for your help.
There's no direct, built-in way (well, okay, perhaps there is — see below), but if you use the callback feature of the replace function, where the replacement can be a function rather than a string (the return value is what's substituted in), you can implement this fairly easily.
For instance, suppose you use the Ruby notation #{xyz} for your placeholders. This code loops through those:
var mappings, str;
str = "One #{X} three #{Y} five";
mappings = {
"X": 2,
"Y": 4
};
str = str.replace(/\#\{([^#]+)\}/g, function(match, key) {
var result;
result = mappings[key];
/* ...processing here */
return result;
});
The resulting string is One 2 three 4 five, because #{X} and #{Y} have been replaced via lookup. You can look at the key and see whether it's an expression and needs to be evaluated rather than simply looked up. That evaluation is where your real work comes in.
Now, you could use with and eval to achieve expression support; change the result = mapping[key]; line above to this:
with (mappings) {
result = eval(key);
}
If you feed the string "One #{X} three #{Y} five #{X + Y * 2}" into that, the result is One 2 three 4 five 10 — because 2 + 4 * 2 = 10.
That works because with sticks the given object on top of the scope chain, so it's the first thing checked when resolving an unqualified reference (like X), and eval executes Javascript code — and so can evaluate expressions — and magically does so within the scope in which it's called. But beware; as Eric pointed out, not all operators are the same in various forms of expression, and in particular Javascript interprets ^ to mean "bitwise XOR", not "to the power of". (It doesn't have an exponent operator; you have to use Math.pow.)
But you need to be very careful about that sort of thing, both with and eval (each in their own way) can be problematic. But the main issues with with are that it's hard to tell where something comes from or where it will go if you do an assignment, which you're not; and the main issues with eval come from using it to interpret strings you don't control. As long as you keep safeguards in place and are aware of the issues...
Boiling that down into a function:
function evaluate(str, mappings) {
return str.replace(/\#\{([^#]+)\}/g, function(match, key) {
var result;
with (mappings) {
result = eval(key);
}
return result;
});
}
alert(evaluate(
"The expression '(#{X} + #{Y}) * 2' equals '#{(X + Y) * 2}'",
{"X": 2, "Y": 4}
)); // alerts "The expression '(2 + 4) * 2' equals '12'"
alert(evaluate(
"The expression '(#{X} + #{Y}) * 2' equals '#{(X + Y) * 2}'",
{"X": 6, "Y": 3}
)); // alerts "The expression '(6 + 3) * 2' equals '18'"
The only way I can think of to achieve this would be a templating engine such as jTemplates. Also see the answers to this SO question.
Nice question:
function substitutestring(str,vals)
{
var regex = /\[[^\]]*\]/gi;
var matches = str.match(regex);
var processed = [];
for(var i = 0; i<matches.length; i++)
{
var match = matches[i];
processed[match] = match.slice(1,-1);
for(j in vals)
{
processed[match] = processed[match].replace(j,vals[j]);
}
processed[match] = eval("("+processed[match]+")");
}
for(var original in processed)
{
str = str.replace(original,processed[original]);
}
return str;
}
document.write(
substitutestring(
"[x] + [y] = [x+y]",
{"x": 1, "y": 2}
)
);
In ES6 you can now use template strings:
var X = 2, Y = 3;
string = Tom has ${X} apples, Lucy has ${Y} apples. Together they have ${X+Y} apples;

Categories