Regex usage, find string between matching pattern

I’m trying to use a regex scheme to find extract a string sequence between two matching tags


id223.55.66id more text here

the “id” tags remain constant, what changes is the number in between and the text that follows.

How would I use regex to extract the number between “id”?

In the regex tutorial I find only cases if the match is already known.

Is it better to go about solving this problem with a while loop? Maybe using regex to find “id” and keep reading until “id” is found again?

Thanks for your help


You must have looked over the stuff that looks like chicken scratchings on p.2 and p.3: those are non-string regex’s. There are a couple on p.2, and then on p.3 the tutorial goes through a phone number matching example using only regex symbols. In any case, here are some more js regex resources:

Regexes require precise specifications of what you are trying to match, and there is a lot of ambiguity in what you said you want to match. But, here is one that will match anything in a string that is between id and id:

var regex = /id.*id/;

Here’s the breakdown ( the / and / are like quotes for a string and are not part of the regex):

i matches the letter i
d matches the letter d
. matches any character

  •   matches the previous character 0 or more times

i matches i
d matches d

So, the regex looks for an i, followed by a d, followed by any character 0 or more times, followed by an i, followed by a d.

The matching behaviour of a regex like that can sometimes be confusing. For instance with this string:


you may think the match would be ‘id1234id’, but the regex has what is called ‘greedy’ behaviour: it tries to get as many characters as possible to match, so the match it produces is actually: ‘id1234idid’.

Oopps, I forgot that you wanted to extract the number. You can put any group of characters in parentheses, and the match() method will put those sub parts in an array. The match() method will return an array with the entire match at index position 0 in the array followed in order by each sub part of the regex that is wrapped in parentheses. So, try this code:

var str =“AAAid223.55.66idXX”
var regex = /id(.*)id/;
var matches = str.match(regex);

Wonderful, I couldn’t have asked for a better explanation. Thank you very much 7stud