0

I have two dateString formats like :date-f1:+2 and :date-f2:-1, if the incoming string is in date-f1:+2 format, I need to console.log as processedDate.format("YYYY-MM-DD"), but if the incoming string is in :date-f2:-1 format, I need to console.log as processedDate.format("DD/MM/YYYY"). I have tried the below, but getting an exception Cannot read properties of null (reading '1')", could someone please advise;

let dateString = ":date-f1:+2";  // or ":date-f2:-1"

    function datesParse(dateString) {
        
    let matchFormat = dateString.match(/^:date-f1:(|):date-f2:(?:([\-\+])([0-9]+)+)?$/);

        let processedDate = moment();
        if (matchFormat[1] === "-") {
            processedDate = processedDate.subtract(matchFormat[2], 'days');
            console.log("Date::"+processedDate.format("DD/MM/YYYY"));
        } else if (matchFormat[1] === "+") {
            processedDate = processedDate.add(matchFormat[2], 'days');
            console.log("Date::"+processedDate.format("DD/MM/YYYY"));
        } else if (matchFormat[1] === "-") {
            processedDate = processedDate.subtract(matchFormat[2], 'days');
            console.log("Date::"+processedDate.format("YYYY-MM-DD"));
        } else if (matchFormat[1] === "+") {
            processedDate = processedDate.add(matchFormat[2], 'days');
            console.log("Date::"+processedDate.format("YYYY-MM-DD"));
        }
        
    }
    
    datesParse(dateString);
4
  • Your OR is wrong..shouldn't it be (:date-f1:|:date-f2:) ? Commented Apr 20, 2022 at 5:59
  • If there are only two specific formats(:date-f1:+2 and :date-f2:-1) where literally nothing changes, then instead of regex use if else else for checking. Using regex here seems wasteful. Commented Apr 20, 2022 at 6:04
  • Agreed, but if we are using includes like below, then we need to check -f1 and + or - operator at the same time ..right ? let word = "-f1" let operator = "+" if (dateString.includes(word) && dateString.includes(operator)) { .... Commented Apr 20, 2022 at 6:14
  • So the operators after f1/f2 changes? like can there be 'f1:+number' and 'f1:-number' the same for f2)? and you want to capture the operator and the word? These weren't mentioned in the question, so please update the question. Commented Apr 20, 2022 at 6:38

1 Answer 1

1

I modified your regex and also added named capture groups, try this: /^:date-(?<word>f[12]):(?:(?<operator>[-+])(?<number>[0-9]+)+)?$/gm

Test here: https://regex101.com/r/cNNHA5/1

With capture groups you can access each of those captured elements easily:

let line = ':date-f1:+2';

let pattern = /^:date-(?<word>f[12]):(?:(?<operator>[-+])(?<number>[0-9]+)+)?$/gm

let match = pattern.exec(line)

console.log('word', match.groups.word)
console.log('operator', match.groups.operator)
console.log('number', match.groups.number)

and use them for checking like:

if (dateString.includes(match.groups.word) && dateString.includes(match.groups.operator))

so you dont have to create extra vars word and operator.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I have upvoted and accepted, how can we build a regex patter
@soccerway to create a regex literal you can do const re = /ab+c/gm; to create a constructor use const re = new RegExp('ab+c');. Read more about regex here: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.