I would like to extract some text between two points in a string, in Javascript
Say the string is
"start-extractThis-234"
The numbers at the end can be any number, but the hyphens are always present.
Ideally I think capturing between the two hypens should be ok.
I would like the result of the regex to be
extractThis
I would like to extract some text between two points in a string, in Javascript
Say the string is
"start-extractThis-234"
The numbers at the end can be any number, but the hyphens are always present.
Ideally I think capturing between the two hypens should be ok.
I would like the result of the regex to be
extractThis
Share
Improve this question
edited Apr 22, 2010 at 15:14
SilentGhost
320k67 gold badges310 silver badges294 bronze badges
asked Apr 22, 2010 at 15:11
Chris BarryChris Barry
4,5947 gold badges57 silver badges90 bronze badges
1
- Wow, I wonder how many of my old questions I'll be embarrised about like this one! – Chris Barry Commented Aug 18, 2012 at 10:23
3 Answers
Reset to default 4string = "start-extractThis-234"
console.log( string.match( '-(.*)-' )[1] );
//returns extractThis
why not just do
var toExtract = "start-extractThis-234";
var extracted = null;
var split = toExtract.split("-");
if(split.length === 3){
extracted = split[1];
}
^.+?-(.+?)-\d+$