or C# style:
String.prototype.$ = function() {
ret = this
for(i=0; i r == "hh, hello, ff"
yes you are right
i wonder if theres a clever way round this ...
On Jul 27, 7:06 pm, "Michael Geary" <[EMAIL PROTECTED]> wrote:
> > > > From:weepy
>
> > > > $s = function(s) {
> > > > p = s.replace(/#{/g, "' + eval(").replace(/}/g, ") + '")
> > > > p = "'" + p + "'"
> > > > return eval(p
> > > From: weepy
> > >
> > > $s = function(s) {
> > > p = s.replace(/#{/g, "' + eval(").replace(/}/g, ") + '")
> > > p = "'" + p + "'"
> > > return eval(p)
> > > }
> > >
> > > a="Jonah"
> > > b=30
> > >
> > > $s("#{a} is #{b} years old")
> > > ==> "Jonah is 30 years old"
> > From: Mic
You could also do sth like this:
String.prototype.templatize = function(values)
{
return this.replace(/{\$([^}]+)}/g, function (full_match, group_match)
{
return values[group_match];
});
}
and use it like this:
var tpl = "Flight {$
On Jul 27, 7:11 pm, "Michael Geary" <[EMAIL PROTECTED]> wrote:
> Sorry to be a party pooper, but what happens if you call $s from inside a
> function, using replacement variables that are local to that function? How
> will the "eval" see those variables?
You can, to the best of my knowledge, eval
> From: weepy
>
> Sick and tired of string manipulation in javascript ? I was
>
> Try this nice little function :
>
> $s = function(s) {
> p = s.replace(/#{/g, "' + eval(").replace(/}/g, ") + '")
> p = "'" + p + "'"
> return eval(p)
> }
>
> OK it's not exactly bullet-proof, but it's
another alteration to fix problems with apostrophes
var $s = function(s) {
p = s.replace(/'/g, '"').replace(/#{/g, "' + ").replace(/}/g, " +
'");
p = "'" + p + "'";
return eval(p);
}
this replaces " with ' in your input string, it works best if you use
" " to signify the edge of your strin
hehe
On Jul 27, 11:36 am, David Duymelinck <[EMAIL PROTECTED]> wrote:
> Can't you make it a oneliner?
>
> $s = function(s) {
>return eval("'" + s.replace(/#{/g, "' + ").replace(/}/g, " + '") + "'");
>
> }
>
> more place for bloated functions :)
>
> -- David
>
> weepy schreef:
>
> > or even
>
Can't you make it a oneliner?
$s = function(s) {
return eval("'" + s.replace(/#{/g, "' + ").replace(/}/g, " + '") + "'");
}
more place for bloated functions :)
-- David
weepy schreef:
or even
$s = function(s) {
p = s.replace(/#{/g, "' + ").replace(/}/g, " + '")
p = "'" + p + "'"
ret
or even
$s = function(s) {
p = s.replace(/#{/g, "' + ").replace(/}/g, " + '")
p = "'" + p + "'"
return eval(p)
}
On Jul 27, 12:09 pm, weepy <[EMAIL PROTECTED]> wrote:
> $s = function(s) {
> p = s.replace(/#{/g, "' + eval(").replace(/}/g, ") + '")
> p = "'" + p + "'"
> return eval(p)
> }
i almost went cross-eyed matching up the ' and " chars in that
s.replace() call. It's almost as readable as Perl cod
11 matches
Mail list logo