Skip to content Skip to sidebar Skip to footer

Regex: Finding The Correct Occurrence Order Of Some Given Special Characters In A String

I want a regular expression that would be able to find the correct occurrence order of * | . | # | 'any HTML element type' like 'p' or 'div' LIKE var regex = //Some regular expr

Solution 1:

You might be better off matching # and . along with their respective values and filtering the results afterwards:

var regex = /\*|([#.]\w+)|(\w+)/g
 matches = pattern_1.match(regex).map(function(x) { return x.match(/^[#.]/) ? x.charAt(0) : x })

or remove the id/class names first, and then match:

 matches = pattern_1.replace(/([#.])\w+/g, "$1").match(/[.#*]|\w+/g)

Solution 2:

ok I have:

/(\*)|(\.)|([a-zA-Z]+)|(#)/g

the problem with this is that unless you specify all the possible html elements that you can capture like div, span etc then it will match all strings including class and id names.

Hope this helps anyway.

Regular expression visualization

Edit live on Debuggex

Post a Comment for "Regex: Finding The Correct Occurrence Order Of Some Given Special Characters In A String"