1
function getStatementValue(view, statement){ // vars "@extends('parentview') ", 'extends'
    var parentname = "";

    view.replace(new RegExp(/\B@\w+\('([^(')\W]+)'\)\s/, 'm'), function(occurance){
        parentname = occurance.replace('@' + statement + '(\'', '')
            .replace('\')', "");
        console.log(parentname) // parentview
        console.log(parentname + 'test') // testntview <- unexpected result
    });

    return parentname;
}

I've got no clue to how that result is appearing. when I add the string as shown in console.log, it replaces the string from the beginning, almost like it's re-assigning the memory space. Is this supposed to be happening? How do I return the correct parentviewtest result?

8
  • 1
    your code, as is, with those arguments (I assume that's what you mean in the comment on the top line) results in parentname never changing - i.e. function(occurance) doesn't seem to get called, ever - Commented Nov 13, 2017 at 4:07
  • @JaromandaX fixed the arg, just added a space. It is getting called because console.log is being fired but my concern is why is the string appending from the start and replacing the string? Commented Nov 13, 2017 at 4:09
  • 1
    it's not ... my guess is parentname is "parentview\r" before you add test ... so, it becomes parentview\rtest which outputs as testntview Commented Nov 13, 2017 at 4:12
  • Interesting, but why would that be happening from the + operator? Commented Nov 13, 2017 at 4:14
  • 1
    it's not ... it's your view argument, I bet there is no "space" at the end of the line, I bet there's a "\r\n" there Commented Nov 13, 2017 at 4:16

1 Answer 1

1
parentname = occurance.replace('@' + statement + '(\'', '')
     .replace('\')', "").trim();

Solved it by adding a .trim() to the string modification. My input included invisible \r and \n characters that I was unaware of.

Thanks to @JaromandaX for spotting that out

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

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.