Convert string to RegExp pattern? 
Author Message
 Convert string to RegExp pattern?

Hi,

I have an input box that requests a path and want to convert it to a
pattern so it can be used by a RegExp.

e.g. I have a string "d:\\" and want to make it into a pattern. The
RegExp pattern is "d\:\\\\" (arrrrrh).

(It seems you can do it the other way round using the source properly of
the RegExp)

The reason I want this is so I can extract a relative path from an
absolute path, for example:

Full path is
"d:\\testfolder\\very\\deep\\folder\\structure\\myFile.xyz"

and I want to replace "d:\\testfolder" with "f:\\test2" so the final
path is

"f:\\test2\\very\\deep\\folder\\structure\\myFile.xyz"

The replace method seemed like a good way to do this, and works fine if
all the paths are hard coded, but when you want to obtain the path from
elsewhere, it seems you need a way to convert it to a pattern.

--
Gerry Hickman (London UK)



Sat, 19 Apr 2003 04:46:50 GMT  
 Convert string to RegExp pattern?
You could use another regular expression to escape the characters before you
use it to create the first regular expression:

  re2 = /([characters to be escaped])/g;
  re1 = new RegExp(strPath.replace(re1, "\\$1"));

=-=-=
Steve
-=-=-


Quote:
> Hi,

> I have an input box that requests a path and want to convert it to a
> pattern so it can be used by a RegExp.

> e.g. I have a string "d:\\" and want to make it into a pattern. The
> RegExp pattern is "d\:\\\\" (arrrrrh).

> (It seems you can do it the other way round using the source properly of
> the RegExp)

> The reason I want this is so I can extract a relative path from an
> absolute path, for example:

> Full path is
> "d:\\testfolder\\very\\deep\\folder\\structure\\myFile.xyz"

> and I want to replace "d:\\testfolder" with "f:\\test2" so the final
> path is

> "f:\\test2\\very\\deep\\folder\\structure\\myFile.xyz"

> The replace method seemed like a good way to do this, and works fine if
> all the paths are hard coded, but when you want to obtain the path from
> elsewhere, it seems you need a way to convert it to a pattern.

> --
> Gerry Hickman (London UK)



Sat, 19 Apr 2003 22:04:50 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Help: Convert VBS RegExp to JS RegExp:ERROR

2. regexp pattern and dictionary obj

3. RegExp patterns

4. RegExp Pattern for Matching a URL?

5. how to make regexp pattern find anything?

6. Variable in a RegExp pattern??

7. RegExp Pattern for Matching a URL?

8. RegExp pattern matching

9. RegExp pattern-matching problem

10. RegExp - Match ANY repeating pattern

11. A RegExp Pattern for removing HTML Tags from a web page

12. help with a RegExp pattern

 

 
Powered by phpBB® Forum Software