Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: String Parser

  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Sydney, NSW, Australia
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    String Parser

    Code:
    function qq(str) {
      while (str.indexOf('$') != -1) {
        str = str.replace(/\$[^\s\W]+/,eval(str.match(/\$([^\s\W]+)/,"$1")[1]));
      }
      return str;
    }
    Usage:
    var greet = 'hello'; var name = 'john';
    alert(qq('$greet there $name!'));

    Not overly useful, I just hate having to 'string '+var+' more strings' all the time :P

    There are some limitations (vars must be global, etc), but maybe someone will find it helpful. I certainly have

    - Mark

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    U.S. (Wish Japan though)
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, it is interesting to say the least... nice work...

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There are some limitations (vars must be global, etc), but maybe someone will find it helpful. I certainly have
    There is a way to make local variables and still use your function:

    Code:
    <html>
        <head>
            <title>Testing</title>
            <script type="text/javascript">
                function qq(str)
                    {
                    while (str.indexOf('$') != -1)
                        {
                        str = str.replace(/\$[^\s\W]+/,eval(str.match(/\$([^\s\W]+)/,"$1")[1]));
                        }
                    return str;
                    }
            </script>
        </head>
        <body>
            <script type="text/javascript">
            var a = "A"
            var b = "B"
            document.write(qq('$a but not $b'))
            </script>
            <hr />
            <script type="text/javascript">
                function aTest()
                    {
                    this.c = "C"
                    this.d = "D"
                    this.qq = qq;
                    document.write(qq('$c but not $d'))
                    }
                aTest()
            </script>
        </body>
    </html>
    Rather than declare local variables using the var keyword, instead make them as objects of the function. Also make the qq function a property of the local function and then you can use it to show the contents of those localized variables.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: String Parser

    I'm a firm believer that eval() is inherently evil. So I would like to point out an alternative to it for this interesting function:

    Code:
    function qq(str) {
        while (str.indexOf('$') != -1) {
            str = str.replace(/\$[^\s\W]+/, this[str.match(/\$([^\s\W]+)/, "$1")[1]]);
        }
        return str;
    }
    When this function is called from the global scope, then "this" will point to window - in which all global variables are contained. If you call qq from a function, then it will try to find properties of that function instead, as outlined above by Roy Sinclair.

    Just thinking about it, but have you also considered doing a global replace with backreference to a matched subpattern?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •