Robins Gupta
Robins Gupta

Reputation: 3153

Javascript regular expression weird behavior String.match()

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.

Upvotes: 2

Views: 141

Answers (1)

Blender
Blender

Reputation: 298532

#? matches at every position in the string, as empty strings are still considered matches. . matches #? before it can match \. and you end up with a result of empty strings and a hash if there is one.

If you're trying to parse CSS selectors, don't use regex. Just write your own parser.

Upvotes: 2

Related Questions