Forum Discussion

Pakhi's avatar
Pakhi
New Contributor III
4 years ago

Filename.replace(/^.*[\\\/]/, '') -: explain the expression

Hi can anyone make me understand the below expression.

FileName.replace(/^.*[\/]/, ‘’)

4 Replies

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    Hey @Pakhi,

    This expression will replace the element that is matched by the regex (/^.*[/]/), with an empty string ''

    • Pakhi's avatar
      Pakhi
      New Contributor III

      Hey can you explain this regex please if you know

      • bojanvelevski's avatar
        bojanvelevski
        Valued Contributor

        Sure.

          . - matches any character except a line break
          * - means 0 or more of the preceding token
          [ ] - means a character set
          \/ - is practically an escaped forward slash character - /
        
  • Basically, the expression removes anything from the beginning of the string to the last forward slash or backslash. In other words, get only the filename from the input string.

    See some examples in the screenshot below. The highlighted parts are matched with ^.*[\\\/] and will be removed.