Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-23-2002, 03:25 AM   PM User | #1
Astro-Boy
New Coder

 
Join Date: Jun 2002
Location: Sydney, NSW, Australia
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Astro-Boy is an unknown quantity at this point
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
__________________
[ AstroBoy Online | Experiments in Stuff | Google, use it! ]
Astro-Boy is offline   Reply With Quote
Old 07-23-2002, 05:26 AM   PM User | #2
Cloudski
Regular Coder

 
Join Date: Jul 2002
Location: U.S. (Wish Japan though)
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Cloudski is an unknown quantity at this point
Well, it is interesting to say the least... nice work...
Cloudski is offline   Reply With Quote
Old 07-24-2002, 12:56 AM   PM User | #3
Roy Sinclair
Senior Coder

 
Join Date: Jun 2002
Location: Wichita
Posts: 3,880
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Sinclair will become famous soon enough
Quote:
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.
Roy Sinclair is offline   Reply With Quote
Old 07-24-2002, 11:04 AM   PM User | #4
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
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?
mordred is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:49 AM.


Advertisement
Log in to turn off these ads.