• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular expression can not understand.

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this code , I know [^0-9], but I can not understand the ".*" which is written in both side of [^0-9], and also I can not understand \ here, I know "\d" (0-9), but why extra "\" with \d? I can not understand.. please tell me.
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As backslashes ('\') are extremely common in regular expressions so JavaScript allows you to quote then in slashes ('/'):

that way every backslash needed in the regex doesn't have to be prefixed with an escape backslash - which is necessary when the regex is inside a JavaScript string.

In a regex
  • '.' matches almost every single character
  • '*' indicates a repetition of 0 or more times.
  • '[^0-9]' is a character class. Since it starts with a caret ('^') it is a negated character class and only matches characters that are not specified.


  • You can quickly experiment with regular expressions on a site like refiddle.
     
    Author
    Posts: 22
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In this code:



    The first backslash is an escaping of the second one backslash, to keep it present, because JS string literals are processed for escape characters. So the contents of the string will actually be a \, then a d, as separate characters.

    Once that string is processed as a regexp, the \d is a character that tells regex to match a single number character. The {16} says repeat it 16 times.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic