Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    glz is offline
    New Coder
    Join Date
    Apr 2007
    Thanked 0 Times in 0 Posts

    Unhappy Regex.Replace syntax error from RegexBuddy code

    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.


  • #2
    Master Coder
    Join Date
    Feb 2003
    Umeå, Sweden
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts