I need to write a regular expression that finds javascript files that match
<anypath><slash>js<slash><anything>.js
For example, it should work for both :
- c:\mysite\js\common.js (Windows)
- /var/www/mysite/js/common.js (UNIX)
The problem is that the file separator in Windows is not being properly escaped :
pattern = Pattern.compile( '^(.+?)' + File.separator + 'js' + File.separator + '(.+?).js$' );
Throwing
java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence
Is there any way to use a common regular expression that works in both Windows and UNIX systems ?
Does
Pattern.quote(File.separator)do the trick?EDIT: This is available as of Java 1.5 or later. For 1.4, you need to simply escape the file separator char:
Escaping punctuation characters will not break anything, but escaping letters or numbers unconditionally will either change them to their special meaning or lead to a PatternSyntaxException. (Thanks Alan M for pointing this out in the comments!)