pattern = "p.class1.class2#id1";
regex = /#?|\.+/g;
pattern.match(regex) ;
//Outputs ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "#", "", "", "", ""]
//Now If I change regex to something like this
regex = /#|\.+/g ;
pattern.match(regex); //Then it gives the correct output
//Outputs [".", ".", "#"]
//Again If I change regex to something like this
regex = /\#|\.*/g;
pattern.match(regex); //Then it again shows the weird behavior
//Outputs ["", ".", "", "", "", "", "", "", ".", "", "", "", "", "", "", "#", "", "", "", ""]
//and at last with this regex combination
regex = /\#?|\.*/g;
pattern.match(regex) ; //Its agains outputs something odd
//Outputs ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "#", "", "", "", ""];
What I need actually to print the correct order of # and . from a given pattern string, where
# : Is optional i.e repeat 0 or 1 times
. : Would repeat 0 or more times
Since # and . could be present in any order in the pattern and all I want Is to print the right order a/c to their occurrence in the string, so can't rely on () capturing groups in this case.
Some more patterns are:
pattern_1 = "p#id.class1.class2"` `//Correct output ['#','.','.']
pattern_2 = ".class1#id.class2"` `//Correct output ['.','#','.']
pattern_3 = ".class1.class2"` `//Correct output ['.','.']
I am getting this output by using regex = /#|\.+/g but don't know what really happening when i am using these regex = /#?|\.+/g or regex = /#?|\.*/g
Please explain.
#?and there is no#it will try to match 0 times of#which returns empty string. No what if you had#?|\., it will try if there is#, or.or empty string. That's what's happening, most of the times, you don't want empty string, so you just remove?and end up using#|\.. I got ride from+since it means match one or more times the previous token.