undefined
undefined

Reputation: 4135

How does this regexp work?

RegExes give me headaches. I have a very simple regex but I don't understand how it works.

The code:

var str= "startBlablablablablaend";
var regex = /start(.*?)end/;
var match = str.match(regex);
console.log( match[0] ); //startBlablablablablaend
console.log( match[1] ); //Blablablablabla

What I ultimately want would be the second one, in other words the text between the two delimiters (start,end).

My questions:

For the last question, what I mean:

var str = "startBla1end startBla2end startBla3end";
var regex = /start(.*?)end/gmi;
var match = str.match(regex);
console.log( match ); // [ "startBla1end" , "startBla2end" , "startBla3end" ]

What I need is:

console.log( match ); // [ "Bla1" , "Bla2" , "Bla3" ];

Thanks :)

Upvotes: 0

Views: 73

Answers (3)

nu11p01n73R
nu11p01n73R

Reputation: 26667

How does it work?

  • start matches start in the string

  • (.*?) non greedy match for character

  • end matches the end in the string

Matching

startBlablablablablaend
  |
start

startBlablablablablaend
     |
     .

startBlablablablablaend
      |
      .

# and so on since quantifier * matches any number of character. ? makes the match non greedy

startBlablablablablaend
                     |
                    end

Why does it match two different things?

It doesnt match 2 differnt things

  • match[0] will contain the entire match

  • match[1] will contain the first capture group (the part matched in the first paranthesis)

Is there a better way to get match[1]?

Short answer No

If you are using languages other than javascript. its possible using look arounds

(?<=start)(.*?)(?=end)
#Blablablablabla

Note This wont work with javascript as it doesnt support negative lookbehinds

Last Question

The best that you can get from a single match statement would be

var str = "startBla1end startBla2end startBla3end";
var regex = /start(.*?)(?=end)/gmi;
var match = str.match(regex);
console.log( match ); // [ "startBla" , "startBla2" , "startBla3" ]

Upvotes: 4

jrel
jrel

Reputation: 317

To solve your last question, you can split up your string and iterate:

var str = "startBla1end startBla2end startBla3end";
var str_array = str.split(" "); 

Then iterate over each element of the str_array using your existing code to extract each Bla# substring.

Upvotes: 1

Mohit Pandey
Mohit Pandey

Reputation: 3833

You need not to do a much effort on it.

Try this this regex:

start(.*)end

You can look at this stackoverflow question which already been answered before.

Regular Expression to get a string between two strings in Javascript

Hope it helps.

Upvotes: 1

Related Questions