Regex extract string after the second "." dot character at the end of a string
I want to extract the second "file extension" of a file name so:
/Users/path/my-path/super.lol.wtf/main.config.js
What I need is:
.config.js
What would be totally awesome if I got an array with 2 strings in return:
var output = ['main', '.config.js'];
What I have tried:
^(\d+\.\d+\.)
But that ain't working.
Thanks
Answer
You could use the following:
([^\/.\s]+)(\.[^\/\s]+)$
([^\/.\s]+)
- Capturing group excluding the characters/
and.
literally as well as any white space character(s) one or more times.(\.[^\/\s]+)
- Similar to the expression above; capturing group starting with the.
character literally; excluding the characters/
and.
literally as well as any white space character(s) one or more times.$
- End of a line
Alternatively, you could also use the following:
(?:.*\/|^)([^\/.\s]+)(\.[^\/\s]+)$
(?:.*\/|^)
- Same as the expression above, except this will narrow the match down to reduce the number of steps. It's a non-capturing group that will either match the beginning of the line or at the/
character.
The first expression is shorter, but the second one has better performance.
Both expressions would match the following:
['main', '.config.js']
In each of the following:
/Users/path/my-path/some.other.ext/main.config.js
some.other.ext/main.config.js
main.config.js
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM