最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Regular expression to extract text between two sets of characters (Javascript) - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 4
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)

  1. 暂无评论