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..
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]);
}