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

How can I get the substring using regular expressions?

I have a string like so:

https://test/api/files/123/versions/1/watermark_content=123456

And I would like to separate this into:

https://test/api/files/123/versions/1/

and

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

watermark_content=123456

How can I achieve this using regular expressions? Or is there an even better/simpler way to do this without regular expressions?

>Solution :

This splits the two parts:

const href = 'https://test/api/files/123/versions/1/watermark_content=123456';

const matches = href.match(/^((?![/]watermark_content=.*).*[/])(watermark_content=[^/]*)/);

console.log(matches[1]);
console.log(matches[2]);

I’m assuming the "right side" (matches[2]) should be a path node that starts with "watermark_content=".

^((?![/]watermark_content=.*).*[/]) matches everything that doesn’t have "watermark_content=…". And should end with a "/".

(watermark_content=[^/]*) matches "watermark_content=" and everything next to it until a "/" (excluding it, and that doesn’t mean "/" should exist).

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