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.
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?
CodePudding user response:
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.