RegExp match of string with square bracket 
Author Message
 RegExp match of string with square bracket

I'm trying to use the replace method on the string object to replace
strings with square bracket arount them.

For example, if I want to replace the string [amount] with $125.00 in
the following string:

"Your loan amount is [amount] plus interest"

How would I build my regular expression?  The square bracket have a
special meaning but padding them with the \ character doesn't seem to
make a difference.

Thanks!



Wed, 15 Dec 2004 07:08:54 GMT  
 RegExp match of string with square bracket
If you have JScript 5.5 or higher installed, here's some code that lets you
substitute any expression in ASP-like fashion from a template file:

var fso = Server.CreateObject("Scripting.FileSystemObject");
var ts = fso.OpenTextFile("c:\\path\\to\\template.txt");
var text = ts.ReadAll();
ts.Close();
text = text.replace( /\<\%(=?)((.|\n)*?)\%\>/g,
        function(ss,eq,ex){return eq? eval(ex):(eval(ex),"");});

A template.txt file might look like:

The amount you entered was <%=Request.Form("amount").Item%>,
the current time is <%=(new Date).toString()%>.

If you want you can declare variables above the block of code that does the
substitution and they can be used in the template.


Quote:
> I'm trying to use the replace method on the string object to replace
> strings with square bracket arount them.

> For example, if I want to replace the string [amount] with $125.00 in
> the following string:

> "Your loan amount is [amount] plus interest"

> How would I build my regular expression?  The square bracket have a
> special meaning but padding them with the \ character doesn't seem to
> make a difference.

> Thanks!



Fri, 17 Dec 2004 03:54:33 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. What's with the square brackets?

2. RegExp: how NOT to match string??

3. Regexp to match a subset of strings but exclude all of a certain character

4. regexp matches unicode char in string

5. RegExp question: match within another match

6. retrieving string text from within brackets

7. RegExp Pattern for Matching a URL?

8. RegExp Pattern for Matching a URL?

9. RegExp pattern matching

10. RegExp pattern-matching problem

11. RegExp to match complete words

12. RegExp whitespace matching

 

 
Powered by phpBB® Forum Software