Results 1 to 3 of 3

Thread: Reg Exp: Newbie Question

  1. #1
    Join Date
    Sep 2009

    Reg Exp: Newbie Question


    Sorry, a newbie question here:

    I encountered the following code:

    var temp = somestring;
    temp = temp.replace( /^\s*/, "" );
    temp = temp.substring(0,6);

    Doesn't this mean replace zero or more occurrences of non-whitespace with the empty string?

    If so, I don't see how the 3rd line of code will ever return anything but whitespace. Am I off the mark?

  2. #2
    Join Date
    Jun 2003
    That's something confusing with regExps, the carat can either represent the start of a line(or in multiline mode the start of the whole string) OR mean not.
    In general if it comes first it means the former, if it occurs later on it means the later, but there are several complex rules that define this.
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  3. #3
    Join Date
    Sep 2009
    I read up on it - it seems that the big difference is whether the caret is used inside square brackets or outside. Inside it negates the character class. Outside it marks the beginning of the string. So in my example the expression means "trim all whitespace from the beginning of the line".

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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