Capturing a word in front of a specific character
I need to create a javascript regex that will capture the "word" that precedes the single or double :
.
Here are some examples:
*, ::before, ::after // do not capture anything
.class1, .class2:before,.class3::after // captures .class2 and .class3
.class4::before // captures .class4
This is what I have right now: /(\S+?):/g
. It matches any non-white space symbol from one to infinity times several times and then stops at :
.
This works except:
- If there is no space before the word, it grabs too far.
- It captures the first colon
::before
and::after
.
+1
source to share
2 answers