开发者

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

开发者 https://www.devze.com 2022-12-28 00:31 出处:网络
I would like to extract some text between t开发者_StackOverflowwo points in a string, in Javascript

I would like to extract some text between t开发者_StackOverflowwo 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+$
0

精彩评论

暂无评论...
验证码 换一张
取 消