www.webdeveloper.com
Results 1 to 4 of 4

Thread: what does a character followed by .* mean in regex (JS)

  1. #1
    Join Date
    Aug 2014
    Posts
    323

    what does a character followed by .* mean in regex (JS)

    If i test the below regex:

    Code:
    .*\/
    on the below string:

    Code:
    http://google.com/vocab/prefix#Billy
    it matches until
    Code:
    http://google.com/vocab/
    Why does't the regex just stop at the first instance of
    Code:
    /
    ?

    does the
    Code:
    \/
    at the end mean

    stop only at the last instance of this character ?

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,762
    https://developer.mozilla.org/en-US/...ar_Expressions

    * Matches the preceding expression 0 or more times. Equivalent to {0,}.

    For example, /bo*/ matches 'boooo' in "A ghost booooed" and 'b' in "A bird warbled" but nothing in "A goat grunted".
    Not so sure about the second statement in the last line as its looking for bo* not b*
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Oct 2017
    Posts
    15
    Operator * is default greedy it means it tries to match as much as possible. To switch to lazy version use *?
    .*?\/

  4. #4
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    583
    The regex looks for maximum closure (or "greedy"). If you are looking for the first slash, try:
    Code:
    [^\/]*\/
    Now you are looking for any number of characters other than a slash that end in a slash.

    HTH

Thread Information

Users Browsing this Thread

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

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

"

"