Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regular expression to find all occurrences of words between substrings

I have a huge string (it’s the inner HTML of the whole webpage).

There are many sub strings:

href="/department/company/3434?        
href="/department/company/254888?    
href="/department/company/87879? 

etc..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How to get all occurrences between "href="/department/company/ and "?" ??
So, basically i need to get all ids which are between the sub strings above.

The result:

arr = [3434, 254888, 87879];

>Solution :

You can use match all with a regular expression to extract this out:

const testString = `
some other stuff here href="/department/company/3434?        
href="/department/company/254888?    <p>Yes indeed</p>
<h1 href="/department/company/87879?>A title or something</h1> 
`;

const matches = testString.matchAll(/department\/company\/([^?]+)\?/gm);

for (let match of matches) {
  console.log(match[1]);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading