简体   繁体   中英

Regular expression to extract text between two sets of characters (Javascript)

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
string = "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+$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM