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 to extract more than one text from the string with javascript?

I have a string that I am getting dynamically from URL which looks as follows

Text from Link: "MD Sheet Template v4.4.0 2023 11 01 (please share then)"

from the above text, I want to get the following.

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

1. Version  
  - v4.4.0

2. Date 
  - 2023 11 01

I tried it like this

let text = "MD Sheet Template v4.4.0 2023 11 01 (please share then)";

let version;
let date;

version = text.match(/\.(.*?)\v3/i);
console.log('version', version); // null

date = text.match(/\.(.*?)\20/i);
console.log('date', date); // null

How do I solve this issue?

>Solution :

const txt="MD Sheet Template v4.4.0 2023 11 01 (please share then)";

const [rev,date]=txt.match(/\s+(v.+?)\s+(.+?)\s+\(/).slice(1);

console.log("revision",rev);
console.log("date", date);

The regexp demands that the revision string starts with a "v" that needs to be preceded by at least one whitspace character (\s+) and the date follows that after at least one other whitespace character. The date is then limited by another whitespace character group (at least one character) immediately followed by an opening parenthesis ((). Having one combined regexp for both patterns makes it more reliable, as a certain sequence is enforced.

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