View Full Version : Regex.Replace syntax error from RegexBuddy code

06-24-2008, 05:59 PM
Error: syntax error
Line: 26, Column: 30
Source Code:
text = Regex.Replace(text, @"\s+", "");

also tried:

text = Regex.Replace(text, @'\s+', '');

Here's full source:

function getCharDimArr(text, font, fontSizeStr, measureBox) {
//remove all spaces from text
try {
text = Regex.Replace(text, @'\s+', '');
} catch (ArgumentException ex) {
alert('Syntax error in the regular expression in removing spaces in getCharWH() func');

I got the code verbatim from RegexBuddy, how can I fix syntax error?
\s+ is regex shorthand for all space characters.

-thanks http://bstats.freehostia.com/b.gif

06-24-2008, 06:25 PM
The syntax you've shown is not JavaScript. It looks to be some bastardised Java.

In JavaScript:
- The delimiters around regex literals are not @" and " or @' and ', both opening and closing delimiter are /. Flags are placed after the closing delimiter.
- The Replace function is named replace.
- The replace function is a method on strings, not on the RegExp object.
- The replace function takes a first argument being a regex and a second argument being either a static string with PERL-inspired replacement monikers, or a function object.
- Exceptions catches are not typed.