Hi I'm stuck at assignment. 
I want to create a regex based on a string sent to function.
If I send for example dd/mm/yyyy,  I want it to create  (\d\d)[\/](\d\d)[\/](\d\d\d\d)  and if yyyy/mm/dd I need it in reverse so I can later use it for date validation?
Is this even possible?
            Asked
            
        
        
            Active
            
        
            Viewed 57 times
        
    0
            
            
         
    
    
        Jeroen
        
- 1,168
- 1
- 12
- 24
 
    
    
        user3187715
        
- 137
- 12
- 
                    I tried 2 days to think of it with for loops appending, index of, and i got nowhere. Beacuse of that i don't know if it's even posible. I don't need solution i need hint. – user3187715 Jan 23 '17 at 12:02
- 
                    1Javascript is a Turing complete language, so anything is possible :^) – Gregor Menih Jan 23 '17 at 12:04
- 
                    1The simplest (to understand/develop) method would be to loop through each character, if it's a "/" add the `[\/]` part, otherwise add `\d`. Add the brackets `()` with the `[\/]` and at the start/end. – freedomn-m Jan 23 '17 at 12:05
- 
                    The next step would be to use `.replace` instead of a loop to change characters, eg: `x.replace(/m/g, "\\d")`. The least obvious part of this is to add the brackets `()` at the same time as the `[\/]` and add them at the start/end directly. – freedomn-m Jan 23 '17 at 12:10
- 
                    Problem is not just spliting beacuse input can be dot or slash or horizontal line. Thats why I'm stuck :) – user3187715 Jan 23 '17 at 12:36
- 
                    @user3187715 you can't say you want to convert "dd/mm/yyyy" to a regex, then, when people give you suggestions, you add a whole bunch of other requirements such as dots and slashes. so move on from .split, you shouldn't still be "stuck". – freedomn-m Jan 23 '17 at 13:44
- 
                    The task is much more complicated than that. I need to take any input and convert it not just one with slashes – user3187715 Jan 23 '17 at 14:02
1 Answers
0
            
            
        How do you use a variable in a regular expression?
Javascript Regex: How to put a variable inside a regular expression?
I think that's what you are trying to do ? Get user input and use it with RegEx ?
You can use .split() to get an array out of string, and then work with that array. You can use .reverse() and .join() if you need it later.
 
    
    
        Community
        
- 1
- 1