2
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.

1
  • 2
    Well empty string is considered as a match. So when you put #? 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. Commented Aug 13, 2013 at 7:31

1 Answer 1

2

#? 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.

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

Comments

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.