0

I have string it contains some code. I want to make some changes on it. There is a for loop I want to modify with js. The loop:

"for i in 0..10 loop"

I want it to be:

"for (var i=0; i<10; i++)"

It sounds me very complicated. What should be the easiest approach?

5
  • 1
    Should this for loop be executing? Why is it in a string? Commented Dec 29, 2011 at 8:00
  • Not really complicated, assuming the pattern is fixed - for [name of variable] in [start index] .. [end index] loop - bolded are the fixed words. If this is the case just split by space and take the stuff you need. Commented Dec 29, 2011 at 8:00
  • No its not executing. I'm tring to convert the code to js. Commented Dec 29, 2011 at 8:01
  • Wouldn't 0..10 need i<=10 in JS? (Or am I misinterpreting the starting language?) Commented Dec 29, 2011 at 8:27
  • That's how I read it too, and how my initial answer was written, but to each his own I guess. Commented Dec 29, 2011 at 8:37

4 Answers 4

2

Based on my comment here is simple implementation:

var before = "for i in 0..10 loop";
var after = Translate(before);

function Translate(command) {
    var tokens = command.split(" ");
    if (tokens.length != 5)
        return "ERROR: invalid command (wrong number of tokens)";
    if (tokens[0] != "for" || tokens[2] != "in" || tokens[4] != "loop")
        return "ERROR: invalid command (syntax error)";
    var indices = tokens[3].split("..");
    if (indices.length != 2)
        return "ERROR: invalid command (invalid indices format)";
    var startIndex = parseInt(indices[0], 10);
    var endIndex = parseInt(indices[1], 10);
    if (isNaN(startIndex) || isNaN(endIndex))
        return "ERROR: invalid command (illegal indices)";
    var varName = tokens[1];
    return "for (var " + varName + "=" + startIndex + "; " + varName + "<" + endIndex + "; " + varName + "++)";
}

Live test case.

It's not perfect, for example you can further check the variable name is legal i.e. start with English letter, not a reserved word etc.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use regex matching to pull out the parts you are interested in.

var string_to_match = 'for i in 0..10 loop',
    re = /for ([A-z]*) in (\d*)\.\.(\d*) loop/,
    data = re.exec(string_to_match);

// data contains:
// ["for i in 0..10 loop", "i", "0", "10"]

for(i = data[2]; i < data[3]; i++) {
    console.log(i);
}

Comments

1

Here's a way to do it, but probably not the best:

var str = "for i in 0..10 loop",
    str = str.replace("i in ", "(var i="),
    str = str.replace("..", "; i<"),
    str = str.replace(" loop", "; i++)");
console.log(str);

Example.

Comments

0

You can try to match this regular expression ^for (.+) in (\d+)\.\.(\d+) loop$ and replace with for (var \1=\2; \1 < \3; \1++).

Try it on http://www.solmetra.com/scripts/regex/ for example...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.