lua-users home
lua-l archive

Re: Feature request: plain option for gsub

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Aug 22, 2014, at 12:02 AM, Sean Conner <sean@conman.org> wrote:
> Yes, it has a dependency on LPeg. 
Yes, it does indeed :P
function escape( s ) return ( s:gsub( '%p', '%%%1' ) ) end
function replace( s, this, that ) return ( s:gsub( escape( this ), escape( that ) ) ) end
print( replace( 'target string', 'get', 'ball' ) )
print( replace( '100% efficiency with 20% effort', '%', ' per cent' ) )
print( replace( 'ababababababa', 'ababa', 'xx' ) )
> tarball string
> 100 per cent efficiency with 20 per cent effort
> xxbxxba

AltStyle によって変換されたページ (->オリジナル) /