0

I'm trying to get the first character after the pattern.

i.e.

border-top-color
padding-top

/-[a-z]/g

selects:

border[-t]op[-c]olor
padding[-t]op

I want to select:

border-[t]op-[c]olor
padding-[t]op

How do you just get the first character after a selected pattern?

Example Here! :)

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Alex Cory
  • 10,635
  • 10
  • 52
  • 62

3 Answers3

2

To get the t after border-, you usally match with this kind of regex:

border-(.)

You can then extract the submatch:

var characterAfter = str.match(/border-(.)/)[1];

match returns an array with the whole match as first element, and the submatches in the following positions.

To get an array of all the caracters following a dash, use

var charactersAfter = str.match(/-(.)/g).map(function(s){ return s.slice(1) })
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

Just use a capturing group:

"border-top-color".replace(/-([a-z])/g, "-[$1]")

Result:

"border-[t]op-[c]olor"
Qtax
  • 33,241
  • 9
  • 83
  • 121
0

You can use submatching like dystroy said or simply use lookbehind to match it:

/(?<=-)./
bitifet
  • 3,514
  • 15
  • 37